简体   繁体   中英

How do I create multiple console inputs for different variables?

My goal is to have the user be prompted for multiple separate inputs that would store the data in a manner that I could then manipulate.

For example:

Question:
What is your username?
What is your name?
Input:
Sparkeyy
Nelson

I then want to be able to take these and add them / multiply if they're numbers. This is what I have so far. (Also first question so sorry for poor formatting)

import java.util.*;

public class Program{

public static void main(String args[]){

    System.out.println("Enter your username: ");
    Scanner scanner = new Scanner(System.in);
    String username = scanner.nextLine();
    System.out.println("Your username is " + username);

}
    public static void (name){

        System.out.println("Enter your name: ");
        Scanner scanner = new Scanner(System.in);
        String name     = scanner.nextLine();
        System.out.println("Your name is " + name);

}
}

Take a look here, they answered you're question thoroughly:

https://stackoverflow.com/questions/5287538/how-to-get-basic-user-input-for-java

You basically need a Scanner, like you're doing, and then for numbers:

scanner.nextInt();

There are also ways of converting properly formatted Strings to Ints/Doubles .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM