Home > database >  How do I display the first five numbers on a single line and the second five numbers on another line
How do I display the first five numbers on a single line and the second five numbers on another line

Time:09-27

I am currently trying to create a do-while loop, counting from 1 to 10, that displays the first five numbers on the first line and the next five numbers on another line. But whenever I run my code, the 6th iteration and onward print on seperate lines each instead of the same line. If anyone could help me understand the error that I made and how to corret it, I would appreciate it.

import java.text.DecimalFormat;
import java.util.Scanner;

public class Hello_World {  // Declare Class
    
    
    public static void main(String[] args) {    // Main Method
        
        Scanner key = new Scanner(System.in);
        DecimalFormat df = new DecimalFormat("##.##");  // Decimal_Format!
        
        /*
         * While Loop count from 1 to 10
         */
        
        int x = 1;  // Declare and Initialize variable 'X'
        do 
        {
            System.out.print(x   " ");
            x  ;
            
            if (x > 5) {
                System.out.println("");
                continue;
            }
            
        }
        while (x <= 10);
        }
        
    }   // Braces delimit blocks of code!


CodePudding user response:

You only want to print the newline when x is 6. Also, you could use printf to make this a bit cleaner. Something like,

int x = 1; // Declare and Initialize variable 'X'
do {
    if (x == 6) {
        System.out.println();
    }
    System.out.printf("= ", x);
    x  ;
} while (x <= 10);

Outputs

  1   2   3   4   5 
  6   7   8   9  10

CodePudding user response:

The error in your code is with your if-statement

enter image description here

By setting the if statement to be true if x > 5 it will trigger for 6 through 10 which is why you get your numbers above 5 all on a separate line. If you just set the new line to kick in when x == 6 it will only trigger a new line when x is 6 and in no other circumstance.

CodePudding user response:

I prefer a two-loop solution. The outer loop increments through the entire range you want; the inner loop increments through a single line.

int limit = 10;
int perLine = 5;
for (int x = 1; x <= limit; x  = perLine) {
    for (int y = x; y < x   perLine && y <= limit; y  ) {
        System.out.printf("=", y);
    }
    System.out.println();
}

This solution works even if the 'limit' is not an exact multiple of 'perLine'.

I wrote this with a for-loop out of sheer habit, but you could convert it to do-while easily enough.

int limit = 10;
int perLine = 5;
int x = 1;
do {
    int y = x;
    do {
         System.out.printf("=", y);
         y  ;
    } while (y < x   perLine && y <= limit);
    System.out.println();
    x  = perLine;
} while (x <= limit);

However, it's a lot more long-winded that way, so unless this is being done as an exercise in using do-while, I'd use a for-loop.

  •  Tags:  
  • java
  • Related