我试图创建一个计算器,但我不能让它工作,因为我不知道如何获得用户输入。

如何在Java中获得用户输入?


当前回答

你可以像这样使用BufferedReader获取用户输入:

    InputStreamReader inp = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(inp);
    // you will need to import these things.

这就是应用它们的方法

    String name = br.readline(); 

因此,当用户在控制台中输入他的名字时,“字符串名称”将存储该信息。

如果它是一个你想要存储的数字,代码将像这样:

    int x = Integer.parseInt(br.readLine());

希望这能有所帮助!

其他回答

然后,Add在main()旁边抛出IOException

DataInputStream input = new DataInputStream(System.in);
System.out.print("Enter your name");
String name = input.readLine();

可能是这样的……

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

    System.out.println("Enter a number: ");
    int i = reader.nextInt();
    for (int j = 0; j < i; j++)
        System.out.println("I love java");
}
Scanner input=new Scanner(System.in);
int integer=input.nextInt();
String string=input.next();
long longInteger=input.nextLong();
Scanner input = new Scanner(System.in);
String inputval = input.next();

使用JOptionPane就可以实现。

Int a =JOptionPane.showInputDialog(null,"Enter number:");