2015-09-23 22 views
0

我想制作一个程序,用户可以在其中输入矩形的高度和宽度,但是可以使用*和+。这里是我的代码For循环星号和加号(矩形窗体)

class Parallelogram { 
public static void main(String[]args){ 

Scanner x = new Scanner(System.in); 
int w; 
int h; 

System.out.print("Enter the Width "); 
w = x.nextInt(); 
System.out.print("Enter the height "); 
h = x.nextInt(); 

for (int i = 0; i < h; i++) { 

     for (int j = 0; j < w; j++) { 

      System.out.print("*"); 

     } 
     System.out.println(); 
     } 
}} 

输出

MY PROGRAM :     What i want my program to look   
Enter width : 4     Enter width : 4      
Enter height : 5    Enter height : 5 
****       **** 
****       ++++ 
****       **** 
****       ++++ 
****       **** 
+2

您可以用'如果(我%2 == 0)打印阿斯特里克斯其他打印加上' – SomeJavaGuy

回答

1

就在您的加模for循环。

这段代码就可以了你:

public static void main(String[] args) { 

    Scanner x = new Scanner(System.in); 
    int w; 
    int h; 

    System.out.print("Enter the Width "); 
    w = x.nextInt(); 
    System.out.print("Enter the height "); 
    h = x.nextInt(); 

    for (int i = 0; i < h; i++) { 

     for (int j = 0; j < w; j++) { 

      if(i % 2 == 0){ 
       System.out.print("*"); 
      }else{ 
       System.out.print("+"); 
      } 

     } 
     System.out.println(); 
    } 
} 
+0

谢谢! + rep :) – Jake