java help printing * and # into a square -
//the question (my code after that)
variable n randomly generated integer. output characters '*' , '#' first row contains stars , last 1 number signs. number of stars decreases in each consecutive row. total number of characters in row n , there n + 1 rows.
for example, if n has value 5, program output:
***** ****# ***## **### *#### ##### //my code below!
random r = new random(); int n = r.nextint(5) + 10; system.out.println("n: "+n); while(n>0){ for(int star = n; star>0; star--){ system.out.print("*"); } for(int hash = 0; hash<n; hash++){ system.out.print("#"); } system.out.println(""); //new line n--; } //my code output - problem: #'s need increase in size 0 rather decrease *'s
**********########## *********######### ********######## *******####### ******###### *****##### ****#### ***### **## *#
what need do:
print n stars, 0 hashes.
print n-1 stars, 1 hash.
print n-2 stars, 2 hashes.
what doing:
print n stars, n hashes.
print n-1 stars, n-1 hash.
print n-2 stars, n-2 hashes.
(n 1 same amount 0 n-1)
so natural thing here create variable outside loop number of hashes print.
or remember original n, since hash count = n - star count.
i hope more giving code.
Comments
Post a Comment