简体   繁体   English

如何为不同的变量创建多个控制台输入?

[英]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 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 . 还有一些将格式正确的字符串转换为Ints / Doubles的方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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