如何在Java控制台中从用户获取简单的键盘输入(整数)? 我之前使用了java.io.*
的方法,但它说已被弃用。
现在应该怎么做?
你可以使用Scanner类
首先导入:
import java.util.Scanner;
Scanner keyboard = new Scanner(System.in);
System.out.println("enter an integer");
int myint = keyboard.nextInt();
小提示:如果你正在使用 nextInt()
和 nextLine()
,那么你可能会遇到一些麻烦,因为 nextInt()
不会读取输入的最后一个换行符,所以 nextLine()
将不能按预期执行。在这个之前的问题中了解如何解决它Skipping nextLine using nextInt。
java.util.Scanner
。 - nachokk你可以像这样使用Scanner类:
import java.util.Scanner;
public class Main{
public static void main(String args[]){
Scanner scan= new Scanner(System.in);
//For string
String text= scan.nextLine();
System.out.println(text);
//for int
int num= scan.nextInt();
System.out.println(num);
}
}
如果您想要验证用户输入,您也可以使用 BufferedReader 来完成这项工作:
import java.io.BufferedReader;
import java.io.InputStreamReader;
class Areas {
public static void main(String args[]){
float PI = 3.1416f;
int r=0;
String rad; //We're going to read all user's text into a String and we try to convert it later
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); //Here you declare your BufferedReader object and instance it.
System.out.println("Radius?");
try{
rad = br.readLine(); //We read from user's input
r = Integer.parseInt(rad); //We validate if "rad" is an integer (if so we skip catch call and continue on the next line, otherwise, we go to it (catch call))
System.out.println("Circle area is: " + PI*r*r + " Perimeter: " +PI*2*r); //If all was right, we print this
}
catch(Exception e){
System.out.println("Write an integer number"); //This is what user will see if he/she write other thing that is not an integer
Areas a = new Areas(); //We call this class again, so user can try it again
//You can also print exception in case you want to see it as follows:
// e.printStackTrace();
}
}
}
因为Scanner类不允许您这样做,或者不那么容易...
而且要验证您使用“try-catch”调用。
Areas.main(null)
的方法,但是如果你想重复使用这段代码,你应该将它放在一个方法中。我在主方法中编写答案的代码是因为我试图制作一个 [mcve],这样任何看到我的答案的人都可以轻松地复制粘贴并查看它的功能,而不需要额外的工作。 - FrakcoolScanner
可以完成所有这些操作,而且不需要 try-catch
结构:如果你要使用 try-catch
,至少应该捕获特定的 NumberFormatException
,而不是仅仅捕获 Exception
。 - user207421要从键盘(标准输入)读取,您可以使用java.util包中的Scanner类。
Scanner类用于获取原始类型(如int、double等)和字符串的输入。尽管不是非常高效,但它是在Java程序中读取输入的最简单的方法。
例如,此代码允许用户从System.in读取数字:
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
Scanner
类中的一些公共方法:
hasNext()
如果此扫描器在其输入中有另一个标记,则返回true。nextInt()
将输入的下一个标记扫描为int。nextFloat()
将输入的下一个标记扫描为float。nextLine()
将此扫描器推进到当前行之后,并返回跳过的输入。nextDouble()
将输入的下一个标记扫描为double。close()
关闭此扫描器。示例:
import java.util.Scanner; //importing class
class ScannerTest {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in); // Scanner object
System.out.println("Enter your rollno");
int rollno = sc.nextInt();
System.out.println("Enter your name");
String name = sc.next();
System.out.println("Enter your fee");
double fee = sc.nextDouble();
System.out.println("Rollno:" + rollno + " name:" + name + " fee:" + fee);
sc.close(); // closing object
}
}
Scanner input = new Scanner(System.in);
System.out.print("Enter something > ");
String inputString = input.nextLine();
System.out.print("You entered : ");
System.out.println(inputString);
JOptionPane示例:
String input = JOptionPane.showInputDialog(null,
"Enter some text:");
JOptionPane.showMessageDialog(null,"You entered "+ input);
您将需要这些导入:
import java.util.Scanner;
import javax.swing.JOptionPane;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class GetInputs{
public static void main(String args[]){
//Scanner example
Scanner input = new Scanner(System.in);
System.out.print("Enter something > ");
String inputString = input.nextLine();
System.out.print("You entered : ");
System.out.println(inputString);
//JOptionPane example
String input = JOptionPane.showInputDialog(null,
"Enter some text:");
JOptionPane.showMessageDialog(null,"You entered "+ input);
}
}
导入: import java.util.Scanner;
定义变量:String name; int age;
定义扫描器:Scanner scan = new Scanner(System.in);
如果你想输入:
name = scan.nextLine();
age = scan.nextInt();
如果不再需要扫描器,请关闭它:scan.close();
Console console = System.console();
String str = console.readLine("Please enter the xxxx : ");
请记得做以下事项:
import java.io.Console;
就是这样!
import java.util.Scanner;
Scanner
类的对象:Scanner s = new Scanner(System.in);
int a = Integer.parseInt(s.nextLine());
整数
值。nextInt()
而不是 nextLine()
。 - khatchadimport java.util.Scanner; //import the framework
Scanner input = new Scanner(System.in); //opens a scanner, keyboard
System.out.print("Enter a number: "); //prompt the user
int myInt = input.nextInt(); //store the input from the user
如果您有任何问题,请告诉我。相当自我解释。我已经注释了代码,这样您就可以阅读它。 :)
- Scanner类:位于java.util.*包中,它有许多方法,根据您的输入类型,您可以利用这些方法。a.nextInt() b.nextLong() c.nextFloat() d.nextDouble() e.next() f.nextLine(); 等等...
import java.util.Scanner;
public class MyClass {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a :");
int a = sc.nextInt();
System.out.println("Enter b :");
int b = sc.nextInt();
int c = a + b;
System.out.println("Result: "+c);
}
}
- BufferedReader类:存在于java.io.*包中,它有许多方法,要从键盘读取值,请使用"readLine()":此方法一次读取一行。
import java.io.BufferedReader;
import java.io.*;
public class MyClass {
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new BufferedReader(new InputStreamReader(System.in)));
System.out.println("Enter a :");
int a = Integer.parseInt(br.readLine());
System.out.println("Enter b :");
int b = Integer.parseInt(br.readLine());
int c = a + b;
System.out.println("Result: "+c);
}
}