2014-10-28 42 views
0

我正在接受行和列的整數值,然後是字符的字符串值。如果用戶對於以字符「@」的列進入的5行和2的值輸出到用戶應該是:創建具有行,列和字符輸入的形狀以填充形狀的問題

@@

@@

@@

@@

@@

我到目前爲止的代碼如下,這裏的評論是,在這裏我似乎無法得到的循環努力創造與T形他使用輸入的字符爲所需的行數和列數輸入。

import java.util.*; 
public class createAShape 
{ 
    public static void main(String [] args) 
    { 
     Scanner in = new Scanner(System.in); 
     String errorMessage = "Please enter an amount in the range 1 and 25", result = ""; 
     System.out.print("Enter number of rows: "); 
     int r = Integer.parseInt(in.nextLine()); 
     if(r < 1 || r > 25) 
      System.out.println(errorMessage); 
     else 
     { 
      System.out.print("Enter number of columns: "); 
      int c = Integer.parseInt(in.nextLine()); 
      if(c < 1 || c > 25) 
       System.out.println(errorMessage); 
      else 
      { 
       System.out.print("Enter symbol to use: "); 
       String character = in.nextLine(); 
       //continuing from here 
      } 
     } 
     System.out.println(result); 
    } 
} 
+0

其中*是*你的循環?如果您沒有顯示無法使用的代碼,我們無法幫助您更正代碼 – 2014-10-28 11:46:56

回答

1

您需要兩個for循環。一個用於計算行數的外循環,以及一個使用輸入的字符執行行打印的內循環。打印完一行的所有列後,您需要打印一個新行。

像這樣的事情shold做到這一點:

for(int i = 0; i < r; i++) { 
    for(int j = 0; j < c; j++) { 
     System.out.print('@'); 
    } 
    System.out.println(); 
} 
+0

清除它,謝謝! – user3425947 2014-10-28 12:00:16

+0

@ user3425947不客氣! – A4L 2014-10-28 12:24:35