使用嵌套循环和 JOptionPane 创建一个框

Create a box using nested loops and JOptionPane

我需要打印一个第一个数字和第二个数字相等的框,例如:第一个数字 = 3 第二个数字 = 3,它看起来像这样

    ***
    ***
    ***

这是我的代码

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);
        }

    }

}

但我一直只收到这个

****
 for(int x=a; x<=b; x++){ ... }

因为 ab 在这一点上是相等的,你将 运行 这个循环只进行一次。您必须正确使用这两个循环:

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
}

如果行数和列数不同,这个循环也可以工作。所以你不必使用任何支票。