i need to print a box that is equal in first number and second number example: first number = 3 second number = 3 and it will look like this
***
***
***
This is my code
import javax.swing.JOptionPane;
public class box{
public static void main(String[]args){
int a,b;
a=Integer.parseInt(JOptionPane.showInputDialog(null, "Enter first number"));
b=Integer.parseInt(JOptionPane.showInputDialog(null, "Enter second number"));
if(a==b){
for(int x=a; x<=b; x++){
a++;
for(int y=0; y<=x; y++){
System.out.print("*");
}
System.out.println();
}
}
else if(a==b){
}
else{
System.exit(0);
}
}
}
but I keep getting only this
****
for(int x=a; x<=b; x++){ ... }
Since a
and b
are equal at this point you will run this loop only once. You have to use the two loops correctly:
for (int i = 0; i < a; i++) { // you start with zero and as many rows as entered
for (int j = 0; j < b; j++) { // same for columns
System.out.print("*");
}
System.out.println(); // start a new line
}
This loop will also work if the number of rows and columns are not the same. So you do not have to use any checks.