Mike Goedken Mike Goedken - 2 months ago 15
Java Question

JAVA broken while loop

How can I make a program that prompts the user to enter number of rows and columns and output figure resembling hollow rectangle (using "*") formed from these rows and columns using while loops?

Scanner stdin = new Scanner(System.in);

System.out.println("Enter number of rows and columns: ");

int row = stdin.nextInt();
int column = stdin.nextInt();

int m = 1;

while(m <= column)
{
while(m <= row)
{
System.out.println("*\t");
}
System.out.print("*");
m++;
}

Answer

Mike you Have to use diffrent variables for difflent loop control.
create an "n" and it should work

Scanner stdin = new Scanner(System.in);
System.out.println("Enter number of rows and columns: ");

int row = stdin.nextInt();
int column = stdin.nextInt();

int m = 1;
int n = 1;

while(m <= column)
{
    while(n <= row)
    {
        System.out.println("*\t");
        n++;
    }
    System.out.print("*");
    m++;
}
Comments