简体   繁体   中英

Small java program prints invisible newline?

I'm doing an assignment and I am done. This is a simple program that prints out pyramids of chars. However, I can't figure out why the program prints a newline when I never specified it with some input, even if it's meant to: https://i.imgur.com/gPs5oC5.png Why do I have to have an extra newline when printing the pyramid upside down? Where is the newline printed?

import java.util.Scanner;

public class Test23 {

public static void main(String[] args) {

    Scanner input = new Scanner(System.in); 

    boolean state = true;

    String messageL = "Length: ";
    String messageD = "Position: ";
    String messageS = "Shutdown!";

    while(state) {
        int limit = 0;
        int degree;

        System.out.print(messageL);
        int length  = input.nextInt();

        while ((length < 1 && length == -1) || length > 26) {

            if (length == -1 ) {
                System.out.println(messageS + "\n");
                state = false;
                break;

            } else {
                System.out.print(messageL);
                length  = input.nextInt();
            }
        }
        if (!state)
            break;
        System.out.print(messageD);
        degree = input.nextInt();

        while((degree > 1) || (degree < 0)) {
            System.out.print(messageD);
            degree = input.nextInt();
        }

        if (degree == 0) 
            //No newline is needed here for some reason.
            length++;

        else if (degree == 1) 
            limit = length;
            //New line here for the pyramids to print symmetrically.
            //System.out.println("");

        for (int i = 0; i < length; ++i) {
            for (int counter = 0; counter < limit; counter++) {
                char letter = (char)(counter + 'A');
                System.out.print(letter);
            }

            if (degree == 0) 
                limit++;

            else if (degree == 1) 
                limit--;

            System.out.println("");
        }

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

Small java program prints invisible newline?

In your program the last System.out.println(""); causes an extra line at the end of your program, ie while(state) is true at the end, So either you comment the print statement or make your state=false at end.

while(state) {    
    ... 
    System.out.println(""); 
}

The most inner loop won't run if the input is 0. limit will be 0, and hence the loop condition is false. As of this it will print en empty line, proceeding to add 1 too limit and then print chars.

for (int i = 0; i < length; ++i) {
    for (int counter = 0; counter < limit; counter++) {
        char letter = (char)(counter + 'A');

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