簡體   English   中英

JAVA 中斷 while 循環

[英]JAVA broken while loop

如何制作一個程序,提示用戶輸入行數和列數,並使用 while 循環從這些行和列中輸出類似於空心矩形(使用“*”)的圖形?

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

Mike 你必須使用不同的變量來進行不同的循環控制。
創建一個“n”,它應該可以工作

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM