Save JAVA Variables

Asked 2 weeks ago, Updated 2 weeks ago, 2 views

public static void main(String args[]) {
    Scanner scanner = new Scanner(System.in);

    String name = scanner.next(); 
    System.out.println ("Name: " + name);
    int number = scanner.nextInt(); 
    System.out.println ("number: " + number);
    int age = scanner.nextInt(); 
    System.out.println ("age: " +age);
    String sex = scanner.next();
    System.out.println ("gender: " + sex);

    scanner.close();
    }

If you press Enter during input, the previous input will be printed immediately. What should I do if I want to save what I received in a variable and print it out at once so that it doesn't output immediately even if I press Enter during input?

java

2022-09-19 23:23

1 Answers

You're already saving it well in the variable.

String name = scanner.next(); 
int number = scanner.nextInt(); 
int age = scanner.nextInt(); 
String sex = scanner.next();

System.out.println ("number: " + number);
System.out.println ("Name: " + name);
System.out.println ("age: " +age);
System.out.println ("gender: " + sex);

I think you can separate the printouts like this.


2022-09-19 23:23

If you have any answers or tips


© 2022 pinfo. All rights reserved.