2017-10-28 43 views
-1

对于这类任务的java我需要一个程序打印字母E是这样的:莱泰雷在与Asterisk

*** 
* 
*** 
* 
*** 

我需要用这种嵌套循环。这是我迄今为止;

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


     final int NUM_ACROSS = 3; // Number of asterisks to print across. 
     final int NUM_DOWN = 5;  // Number of asterisks to print down. 

     int row; // Loop control for row number. 
     int column; // Loop control for column number. 

     // This is the work done in the detailLoop() method 
     // Write a loop to control the number of rows. 
       for(row = 1; row <= NUM_DOWN; row++) { 
        if(row == 1 || row == 3 || row == 5 || row == NUM_DOWN) 
       for(column = 1; column <= NUM_ACROSS; ++column){ 
        if(column == 2 || column == 4 || column == NUM_ACROSS) 
        // Write a loop to control the number of columns 
       // Decide when to print an asterisk in every column. 
      System.out.print("*");   
      // Decide when to print asterisk in column 1. 
      System.out.print("*"); 
      // Decide when to print a space instead of an asterisk. 
      System.out.print(" "); 
     // Figure out where to place this statement that prints a newline. 
     System.out.println(); 
       } 
       } 
       // This is the work done in the endOfJob() method 
     System.exit(0); 
    } // End of main() method. 

} // End of LetterE class. 

不幸的是,它只是打印

** 
* 
** 
* 
** 

什么我mising?谢谢您的帮助。

+1

欢迎来到Stack Overflow! [你的步调试器告诉你什么?](http://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) –

回答

0

这里是你错过了什么(用注释标记它)。

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


     final int NUM_ACROSS = 3; // Number of asterisks to print across. 
     final int NUM_DOWN = 5;  // Number of asterisks to print down. 

     int row; // Loop control for row number. 
     int column; // Loop control for column number. 

     // This is the work done in the detailLoop() method 
     // Write a loop to control the number of rows. 
     for(row = 1; row <= NUM_DOWN; row++) { 
      if(row == 1 || row == 3 || row == 5 || row == NUM_DOWN) 
       for(column = 1; column <= NUM_ACROSS; ++column) { 
        if (column == 2 || column == 4 || column == NUM_ACROSS) 
         // Write a loop to control the number of columns 
         // Decide when to print an asterisk in every column. 
         System.out.print("*"); 
        } // <<< here it is, you misplaced one curly bracket (delete extra one before System.exit and you're good to go) 
        // Decide when to print asterisk in column 1. 
        System.out.print("*"); 
        // Decide when to print a space instead of an asterisk. 
        System.out.print(" "); 
        // Figure out where to place this statement that prints a newline. 
        System.out.println(); 
       } 
     // This is the work done in the endOfJob() method 
     System.exit(0); 
    } // End of main() method. 

} // End of LetterE class. 
1

在我看来,你是过度复杂这一点。所有你需要做的是交替打印三颗星和一颗星,用奇数行(1,3,5)得到三颗星和偶数行(2,4)得到一颗星。

for (int i=0; i < 5; ++i) { 
    String line = i % 2 == 0 ? "***" : "*"; 
    System.out.println(line); 
} 

如果不得不打印其他字母,那么我们可能要改变我们的设计。在这种情况下,我们可以创建帮助方法来打印每个字母。或者,更好的是,创建辅助方法来生成部分字母。但根据您给我们的要求,上面的答案似乎是合理的。

Demo