public static void main(String[] args) { int r =in.nextInt(); int b =0; int c =1; while (c<=r){ b++; b++; c++; System.out.println(b); } } 

How to change the code to display data in one line? If there are errors in the code please tell me how to do it.

Closed due to the fact that off-topic participants Dmitriy Simushev , Alexey Shimansky , Denis , Bald , aleksandr barakin 7 Oct '16 at 6:42 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reasons:

  • "The question is caused by a problem that is no longer reproduced or typed . Although similar questions may be relevant on this site, solving this question is unlikely to help future visitors. You can usually avoid similar questions by writing and researching a minimum program to reproduce the problem before publishing the question. " - Alexey Shimansky, Bald, aleksandr barakin
  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - Dmitriy Simushev, Denis
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • one
    And you, pardon me, wondered what the System.out.println(b) code was doing? - VladD 5:56 pm
  • Print the variable b to the console. - Rubik
  • one
    @Rubik, well then you should know where the line breaks come from. - Dmitriy Simushev
  • I will take note) - Rubik
  • one
    @Rubik but to change the question in such a way that its meaning is completely lost (at the same time making the existing answers erroneous) - no need - Dmitriy Simushev

3 answers 3

Your program should look like this:

 public static void main(String[] args){ Scanner in = new Scanner(System.in); int n = in.nextInt(); for ( int i = 0; i < n; i++){ if(i % 2 == 0){ System.out.print(i + " "); } } } 

    It is enough to read the manual from Oracle :

    • println - prints a variable and makes a transition to a new line.
    • print - just prints the variable and remains on the same line

    Therefore, do as already suggested here before:

     System.out.print(b + " ") 

    And do not forget to read manuals.

      Single line output

       System.out.print(b + " ");