我试图创建一个计算器,但我不能让它工作,因为我不知道如何获得用户输入。
如何在Java中获得用户输入?
我试图创建一个计算器,但我不能让它工作,因为我不知道如何获得用户输入。
如何在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());
希望这能有所帮助!
其他回答
您可以使用BufferedReader获取用户输入。
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String accStr;
System.out.println("Enter your Account number: ");
accStr = br.readLine();
它将在accStr中存储一个String值,因此您必须使用Integer.parseInt将其解析为int。
int accInt = Integer.parseInt(accStr);
下面是获取键盘输入的方法:
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;
class Daytwo{
public static void main(String[] args){
System.out.println("HelloWorld");
Scanner reader = new Scanner(System.in);
System.out.println("Enter the number ");
int n = reader.nextInt();
System.out.println("You entered " + n);
}
}
Scanner input=new Scanner(System.in); int integer=input.nextInt(); String string=input.next(); long longInteger=input.nextLong();
使用JOptionPane就可以实现。
Int a =JOptionPane.showInputDialog(null,"Enter number:");