2016-10-03 173 views
-5

如何製作一個程序,提示用戶輸入行數和列數,並輸出類似空心矩形(使用「*」)的圖形,這些行和列使用while循環形成?JAVA while 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++; 
    } 
+1

你做到了已經。你現在的問題是什麼? –

+1

只要'row> 1',你的innerloop將永遠運行。不要在內部循環中使用與外部循環相同的變量。 – CollinD

+0

@CollinD我不明白我的內循環如何永遠運行 –

回答

0

邁克你必須使用diffrent變量進行difflent循環控制。
創建一個 「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++; 
} 
+0

非常感謝你!現在我怎樣才能讓m的打印語句顯示在頂部呢? –

+0

當你說:你得到m的打印語句是什麼意思? 我認爲只是System.out.print(m)或System.out.print(n) –

+0

我的歉意我的意思是如何在最後一行打印列中有多少個符號如何做第一行相同在我開始打印行之前..所以它看起來像一個矩形 –