java 获取键盘输入法_java-获取键盘输入

博客围绕Java在控制台获取用户键盘输入(整数)展开,指出原使用的java.io.*已被弃用,介绍了多种解决方案,如使用Scanner类、BufferedReader类,还提及用JOptionPane弹出对话框获取输入,以及Java 6及以上版本可使用Console类。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

java-获取键盘输入

如何在Java的控制台中从用户那里获得简单的键盘输入(整数)? 我使用java.io.*来完成此操作,但它已被弃用。

我现在应该怎么做?

9个解决方案

64 votes

您可以使用扫描仪类

首先导入:

import java.util.Scanner;

然后,您可以像这样使用。

Scanner keyboard = new Scanner(System.in);

System.out.println("enter an integer");

int myint = keyboard.nextInt();

旁注:如果您将nextInt()和nextLine()一起使用,则可能会遇到一些问题,因为nextInt()无法读取输入的最后一个换行符,因此nextLine()不会以期望的方式执行。 在上一个问题中了解更多有关如何解决该问题的信息。使用nextInt跳过nextLine。

nachokk answered 2020-07-25T23:47:48Z

18 votes

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

}

}

Mike B answered 2020-07-25T23:48:08Z

13 votes

如果您想验证用户输入,也可以使用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”调用。

Frakcool answered 2020-07-25T23:48:37Z

6 votes

您可以使用扫描仪类

从键盘读取(标准输入)可以使用Scanner是close()包中的类。

close()软件包,用于获取诸如Scanner等和System.in等原始类型的输入。这是读取Java程序中输入的最简单方法,尽管效率不高。

要创建Scanner类的close(),我们通常将预定义的对象System.in,它代表标准输入流(键盘)。

例如,此代码允许用户从System.in中读取数字:

Scanner sc = new Scanner(System.in);

int i = sc.nextInt();

某些Public方法在close()类中。

close()如果此扫描仪在其扫描仪中还有另一个令牌,则返回true输入。

close()将输入的下一个标记扫描为int。

close()将输入的下一个标记扫描为浮点型。

close()将此扫描仪推进到当前行之外并返回跳过的输入。

close()将输入的下一个标记扫描为双精度。

close()关闭此扫描仪。

有关Scanner类中Public方法的更多详细信息。

例:-

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

}

}

anoopknr answered 2020-07-25T23:49:54Z

3 votes

您可以使用扫描仪获取下一行,并对输入的行进行任何所需的操作。 您还可以使用JOptionPane弹出一个对话框,要求输入。

扫描仪示例:

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;

上面的完整Java类

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);

}

}

s-hunter answered 2020-07-25T23:50:31Z

2 votes

import 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

如果您有任何疑问,请告诉我。 相当不言自明。 我注释了代码,以便您可以阅读。 :)

Dummy Code answered 2020-07-25T23:50:51Z

2 votes

如果您具有Java 6(应该有btw)或更高版本,则只需执行以下操作:

Console console = System.console();

String str = console.readLine("Please enter the xxxx : ");

请记住要做:

import java.io.Console;

而已!

M-D answered 2020-07-25T23:51:20Z

2 votes

进口:scan.close();

定义变量:scan.close();

定义扫描仪:scan.close();

如果要输入:

文字:scan.close();

整数:scan.close();

如果不再需要关闭扫描仪:scan.close();

loxsat answered 2020-07-25T23:52:06Z

1 votes

添加行:

import java.util.Scanner;

然后创建一个integer类的对象:

Scanner s = new Scanner(System.in);

现在您可以随时拨打电话:

int a = Integer.parseInt(s.nextLine());

这将从键盘上存储integer的值。

Yeshdeep Kumar answered 2020-07-25T23:52:39Z

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值