0

我正在尝试编写一个输出空行的方法,然后 90 = 在新行上签名,最后是空行。

这是我的代码...

public void easyToRead(){
    for (int i=0; i<=EQUAL_SIGNS; i++){
        if (i >= 0){
            if (i == EQUAL_SIGNS || i == 0){
                System.out.println(" ");
            }
            else {
                System.out.print("=");
            }
        }
    }
}

输出应该是这样的......

blah blah blah

======================= ---> 90 of them

blah blah blah

有人可以帮我更正我的代码。

4

3 回答 3

3

我认为你在for循环的正确轨道上,但你不应该真的需要任何if陈述。这样的事情可能更简单......

public void easyToRead() {
    // Write a blank line
    System.out.println();

    // Write the 90 equals characters
    for (int i=0; i<90; i++){
        System.out.print("=");
    }

    // Write a new-line character to end the 'equals' line
    System.out.println();

    // Write a blank line
    System.out.println();
}

哪个会输出...

<blank line>
===========================...
<blank line>
// the next output will write on this line

对于您想要的每个空行,只需添加另一个System.out.println();语句

于 2012-05-16T13:40:31.833 回答
2
System.out.println(String.format("%n%90s%n"," ").replaceAll(" ", "="));
于 2012-05-16T13:40:08.993 回答
0

Let's imagine you want to use as many equal signs as you want.

In your class, create a static block

public static String LINE_EQUAL_SIGNS;

public static String LINE_SEPARATOR = System.getProperty("line.separator").toString();

static {

    StringBuffer sb = new StringBuffer(LINE_SEPARATOR);
    for (int i = 0; i < EQUAL_SIGNS; i++)
        sb.add("=");
    sb.add(LINE_SEPARATOR);
    LINE_EQUAL_SIGNS = sb.toString();
}

Now you only need to do :

public void easyToRead()
{
    System.out.print(LINE_EQUAL_SIGNS);
}
于 2012-05-16T13:44:37.597 回答