带有星号的java中的LetterE

时间:2017-10-28 23:22:45

标签: java

对于这个课程作业,我需要让程序打印字母E,如下所示:

<a href "#">A</a> 
   <a href "#">B</a>
   <a href "#">C</a>

我需要使用嵌套循环来实现这一点。这是我到目前为止所拥有的;

***
*
***
*
***

不幸的是,它只是打印

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.

我在做什么?谢谢你的帮助。

2 个答案:

答案 0 :(得分:1)

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

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

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

Demo

答案 1 :(得分: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.