简体   繁体   中英

print until end of line in java

In Java, I am trying to print a word that the user inputs 100 times but instead of having it print each instance on a new line, I am trying to print as many as I can on one line and then go to a new line. Would this be easy to do in java? I am new to java so any help would be greatly appreciated! Here is what I have so far below.

public class Main {

    public static void main(String[] args) {

        String name= "TEST"; //defined for debugging purposes

        int i=0;

        while (i < 100)
        {
            System.out.println(name + " ");
            i++;
        }
}

}

您应该使用System.out.print而不是System.out.println语句在同一行上进行打印。

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

    String name= "TEST"; //defined for debugging purposes

    int i=0;

    while (i < 100)
    {
        System.out.print(name + " ");
        i++;
    }
    System.out.println();
}
}

If you wish to follow the 72 - 80 characters per line (CPL) standard, http://en.wikipedia.org/wiki/Characters_per_line , you could just append your data into a single StringBuilder and count each append as an iteration.

public static void main(String[] args) throws Exception {
    final int CHARACTERS_PER_LINE = 72;

    String name = "TEST"; //defined for debugging purposes
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < 100; i++) {
        sb.append(name).append(" ");

        // Check if the next append exceeds the CPL
        if (sb.length() + name.length() + 1 > CHARACTERS_PER_LINE) {
            System.out.println(sb); // Print the line
            sb.setLength(0); // Clear for a new line
        } 
    }
    // Print what is left
    System.out.println(sb);
}

Results (Each line should have less than or equal to 72 characters):

TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST 
TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST 
TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST 
TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST 
TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST 
TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST 
TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST 
TEST TEST 

If you want neat line breaks; you can schedule them to automatically occur every 10 iterations or so using the modulus % operator.

public class Main {

    public static void main(String[] args) {

        String name= "TEST"; //defined for debugging purposes

        int i=0;

        while (i < 100)
        {
            System.out.print(name + " ");
            if(i%10 == 0 && i != 0) {
                System.out.println();
            }
            i++;
        }
}

Likewise, you can always use the newline character \\n to print on a new line. In java escape sequences in String literals start with the \\ character, followed by an escape character. Doing \\n is a newline, \\t is a tab, \\' is a single quote, \\" double quote, 2 \\, backslash - those are the common ones.

Using newlines & a ternary operator:

while (i < 100)
{
    String line = i%10 == 0 && i != 0 ? name + "\n" : name + " ";
    System.out.print(line);
    i++;
}

If you aren't familiar with the ternary operator; it's a simplified if/else statement. The ternary operator above is the same as saying:

String line = "";
if(i%10 == 0 && i != 0) {
  line = name + "\n";
}
else {
  line = name + " ";
}

That's a simple way of formatting that doesn't take into account character length.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM