How to print the output in two separate lines?

前端 未结 3 769
面向向阳花
面向向阳花 2021-01-24 19:50

I\'ve been trying to print the output in two separate lines, I used System.out.println() and also System.out.println(\"\\n\") but I only seem to be get

3条回答
  •  情歌与酒
    2021-01-24 20:27

    The line:

    String in = input.nextLine();
    

    is capturing your first number entered and it is never added to the list al.

    So, if you enter:

    45

    40

    67

    0

    the output is:

    [40, 67] (using System.out.println(al))

    or:

    4067 (using your for loop).

    Note, the loop is broken by entering 0, not non-numeric characters as the first output text line would suggest.

    Read lines, please enter some other character to stop

    should really read

    Read lines, please enter 0 to stop

    [EDIT]

    To add/display numbers to the list correctly:

    1) Remove the line:

    String in = input.nextLine();
    

    2) Remove the for loop at the end and replace it with:

    System.out.println(al);        
    

提交回复
热议问题