Home > database >  display the numbers 1 to 4 on the same line with each pair of adjacent number separated by one space
display the numbers 1 to 4 on the same line with each pair of adjacent number separated by one space

Time:10-06

can anyone give me a hand with this java question, it is kicking my ass and i know i am overthinking it, you cant ask for the user inputs,

import java.util.Scanner;

public class Question3 {
    public static void main(String[] args) {
        Scanner sc = new Scanner (System.in);
        byte first = 1, secound = 2, third = 3, fourth = 4;
        
        /* System.out.println("1 2 3 4");
        
        System.out.print("1 ");
        System.out.print("2 ");
        System.out.print("3 ");
        System.out.print("4"); */
      
        /* System.out.print(first);
        System.out.print(" ");
        System.out.print(secound);
        System.out.print(" ");
        System.out.print(third " ");
        System.out.print(fourth); */
       
        System.out.print(1);
        System.out.print(" ");
        System.out.print(2);
        System.out.print(" ");
        System.out.print(3);
        System.out.print(" ");
        System.out.println(4);
      
        System.out.printf("\n%d %d %d %d", first, secound, third, fourth);   
    }
}

CodePudding user response:

No need to use so many statements. Just use \n to create a new line

System.out.print("1 \n2 \n3 \n4 ");

CodePudding user response:

Combine these:

System.out.print(3);
System.out.print(" ");

Into this:

System.out.print("3 ");

And make sure your printf statement is removed

  •  Tags:  
  • java
  • Related