views:

40

answers:

3

Hello i need a program that reads a number and then prints a square pattern of (#) hash. But each edge needs the same number of hashes

eg

enter a number: 5

#####
#####
#####
#####
#####

this is what i have so far

import console;

print("write a number: ");
int n = readInt();
int nva=0;
String i="#";

while (nva<n){
   print(i);
   nva=nva + 1;

   }
 println();
+3  A: 

since it is definitely HW i will give direction. Since you have to fill 2D shape you should use nested cycles - one for rows, another for columns. Is it better now?

Edit: You should read manual about loops. for is more suitable here than while

Andrey
I was about to post almost exactly the same thing. +1 :)
JacobM
Nested loops would make this slower than it has to be. Better to just have two loops...one to create the string of n #'s (if even necessary; some languages have the built in ability to do this!), and one to output the string n times.
cHao
but how can i use for, if i am not sure how many time the hash will be printed?
Nathan
`for (nva=0; nva<n; ++nva) { do stuff }`. It works the exact same way you'd use a while loop, except that all the stuff that changes and checks your counter is in the same place (so it's easier to see when/if the loop will ever end, among other reasons).
cHao
A: 

In every iteration of the loop you are printing the # just once.

You'll have to use another loop inside your main loop that prints # n times for each iteration of the outer loop.

codaddict
+1  A: 

... or you could iterate up to n^2 and output a line break after each sequence of n chars.

Alex Ciminian