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

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


当前回答

下面是获取键盘输入的方法:

Scanner scanner = new Scanner (System.in);
System.out.print("Enter your name");  
String name = scanner.next(); // Get what the user types.

其他回答

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        System.out.println("Welcome to the best program in the world! ");
        while (true) {
            System.out.print("Enter a query: ");
            Scanner scan = new Scanner(System.in);
            String s = scan.nextLine();
            if (s.equals("q")) {
                System.out.println("The program is ending now ....");
                break;
            } else  {
                System.out.println("The program is running...");
            }
        }
    }
}

可能是这样的……

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类或Console类

Console console = System.console();
String input = console.readLine("Enter input:");

以下是一个更完善的公认答案,解决了两个常见需求:

重复收集用户输入,直到输入退出值 处理无效输入值(本例中是非整数)

Code

package inputTest;

import java.util.Scanner;
import java.util.InputMismatchException;

public class InputTest {
    public static void main(String args[]) {
        Scanner reader = new Scanner(System.in);
        System.out.println("Please enter integers. Type 0 to exit.");

        boolean done = false;
        while (!done) {
            System.out.print("Enter an integer: ");
            try {
                int n = reader.nextInt();
                if (n == 0) {
                    done = true;
                }
                else {
                    // do something with the input
                    System.out.println("\tThe number entered was: " + n);
                }
            }
            catch (InputMismatchException e) {
                System.out.println("\tInvalid input type (must be an integer)");
                reader.nextLine();  // Clear invalid input from scanner buffer.
            }
        }
        System.out.println("Exiting...");
        reader.close();
    }
}

例子

Please enter integers. Type 0 to exit.
Enter an integer: 12
    The number entered was: 12
Enter an integer: -56
    The number entered was: -56
Enter an integer: 4.2
    Invalid input type (must be an integer)
Enter an integer: but i hate integers
    Invalid input type (must be an integer)
Enter an integer: 3
    The number entered was: 3
Enter an integer: 0
Exiting...

注意,如果没有nextLine(),错误的输入将在无限循环中重复触发相同的异常。您可能希望根据具体情况使用next(),但要知道像这样带有空格的输入将生成多个异常。

你可以像这样使用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());

希望这能有所帮助!