2014-10-28 46 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