Java中如何获取用户输入?
我尝试创建一个计算器,但无法让它工作,因为我不知道如何获取用户输入。
Java中如何获取用户输入?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我尝试创建一个计算器,但无法让它工作,因为我不知道如何获取用户输入。
Java中如何获取用户输入?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(29)
最简单的方法之一是使用
Scanner
对象如下:One of the simplest ways is to use a
Scanner
object as follows:您可以根据需要使用以下任意选项。
扫描仪
类BufferedReader
和InputStreamReader
类DataInputStream
类DataInputStream
类中的readLine
方法已被弃用。要获取字符串值,您应该将之前的解决方案与 BufferedReader 结合使用Console
class显然,这种方法在某些IDE中效果不佳。
You can use any of the following options based on the requirements.
Scanner
classBufferedReader
andInputStreamReader
classesDataInputStream
classThe
readLine
method from theDataInputStream
class has been deprecated. To get String value, you should use the previous solution with BufferedReaderConsole
classApparently, this method does not work well in some IDEs.
您可以使用 Scanner 类或控制台类
You can use the Scanner class or the Console class
您可以使用
BufferedReader
。它将在
accStr
中存储一个String
值,因此您必须使用 int 将其解析为int
。 oracle.com/javase/8/docs/api/java/lang/Integer.html#parseInt-java.lang.String-" rel="noreferrer">Integer.parseInt
。You can get user input using
BufferedReader
.It will store a
String
value inaccStr
so you have to parse it to anint
usingInteger.parseInt
.以下是获取键盘输入的方法:
Here is how you can get the keyboard inputs:
最好的两个选项是 BufferedReader 和 Scanner。
最广泛使用的方法是 Scanner,我个人更喜欢它,因为它简单且易于实现,以及将文本解析为原始数据的强大实用程序。
使用 Scanner 的优点
Scanner
类BufferedInputStream 的优点
Scanner
)总体而言,每种输入法都有不同的用途.
如果您输入大量数据,
BufferedReader
可能会对您更好
如果您输入大量数字
扫描仪
会自动解析这非常方便
对于更基本的用途我会推荐
Scanner
因为它更容易使用并且更容易编写程序。以下是如何创建扫描仪
的快速示例。我将在下面提供如何使用Scanner
的全面示例(有关
BufferedReader
的更多信息,请参阅 如何使用 BufferedReader 并参阅 读取字符行)java.util.Scanner
注意:其他类,例如
Console
和DataInputStream
也是可行的替代方案。Console
具有一些强大的功能,例如然而,并非所有 IDE(例如 Eclipse)都具有读取密码的功能。发生这种情况的原因是 Eclipse 将您的应用程序作为后台进程运行,而不是作为带有系统控制台的顶级进程运行。 这里是一个链接,指向有关如何实现控制台的有用示例 code> 类。
DataInputStream
主要用于以与机器无关的方式从底层输入流中将输入作为原始数据类型读取。DataInputStream
通常用于读取二进制数据。它还提供了读取某些数据类型的便捷方法。例如,它有一个读取 UTF 字符串的方法,其中可以包含任意数量的行。但是,它是一个更复杂的类,更难实现,因此不建议初学者使用。 这里是一个链接,指向如何实现
DataInputStream
.The best two options are
BufferedReader
andScanner
.The most widely used method is
Scanner
and I personally prefer it because of its simplicity and easy implementation, as well as its powerful utility to parse text into primitive data.Advantages of Using Scanner
Scanner
classAdvantages of BufferedInputStream
Scanner
between threads)Overall each input method has different purposes.
If you are inputting large amount of data
BufferedReader
might bebetter for you
If you are inputting lots of numbers
Scanner
does automatic parsingwhich is very convenient
For more basic uses I would recommend the
Scanner
because it is easier to use and easier to write programs with. Here is a quick example of how to create aScanner
. I will provide a comprehensive example below of how to use theScanner
(For more info about
BufferedReader
see How to use a BufferedReader and see Reading lines of Chars)java.util.Scanner
Note: Other classes such as
Console
andDataInputStream
are also viable alternatives.Console
has some powerful features such as ability to read passwords, however, is not available in all IDE's (such as Eclipse). The reason this occurs is because Eclipse runs your application as a background process and not as a top-level process with a system console. Here is a link to a useful example on how to implement theConsole
class.DataInputStream
is primarily used for reading input as a primitive datatype, from an underlying input stream, in a machine-independent way.DataInputStream
is usually used for reading binary data. It also provides convenience methods for reading certain data types. For example, it has a method to read a UTF String which can contain any number of lines within them.However, it is a more complicated class and harder to implement so not recommended for beginners. Here is a link to a useful example how to implement a
DataInputStream
.您可以编写一个简单的程序来询问用户名并打印回复使用的输入内容。
或者要求用户输入两个数字,您可以对这些数字进行加、乘、减或除操作,并打印用户输入的答案,就像计算器的行为一样。
所以你需要 Scanner 类。您必须
import java.util.Scanner;
并且在代码中您需要使用Input是一个变量名。
看看有何不同:
input.next();
、i = input.nextInt();
、d = input.nextDouble();
根据 String,int 和 double 的变化方式与其余的相同。不要忘记代码顶部的 import 语句。
另请参阅博客文章 "扫描仪类并获取用户输入”。
You can make a simple program to ask for user's name and print what ever the reply use inputs.
Or ask user to enter two numbers and you can add, multiply, subtract, or divide those numbers and print the answers for user inputs just like a behavior of a calculator.
So there you need Scanner class. You have to
import java.util.Scanner;
and in the code you need to useInput is a variable name.
See how this differs:
input.next();
,i = input.nextInt();
,d = input.nextDouble();
According to a String, int and a double varies same way for the rest. Don't forget the import statement at the top of your code.
Also see the blog post "Scanner class and getting User Inputs".
要读取一行或字符串,您可以将
BufferedReader
对象与InputStreamReader
对象结合使用,如下所示:To read a line or a string, you can use a
BufferedReader
object combined with anInputStreamReader
one as follows:在这里,程序要求用户输入一个数字。之后,程序打印数字的位数以及数字的总和。
Here, the program asks the user to enter a number. After that, the program prints the digits of the number and the sum of the digits.
这是使用 java.util.Scanner 的问题中的程序:
Here is your program from the question using
java.util.Scanner
:只是一个额外的细节。如果您不想冒内存/资源泄漏的风险,则应该在完成后关闭扫描器流:
请注意,java 1.7 及更高版本将其捕获为编译警告(不要问我是怎么知道的:-)
Just one extra detail. If you don't want to risk a memory/resource leak, you should close the scanner stream when you are finished:
Note that java 1.7 and later catch this as a compile warning (don't ask how I know that :-)
这是已接受答案的更完善版本,可满足两个常见需求:
代码
示例
请注意,如果没有
nextLine()
,错误的输入将在无限循环中重复触发相同的异常。根据具体情况,您可能需要使用next()
来代替,但要知道像this has paths
这样的输入会生成多个异常。Here is a more developed version of the accepted answer that addresses two common needs:
Code
Example
Note that without
nextLine()
, the bad input will trigger the same exception repeatedly in an infinite loop. You might want to usenext()
instead depending on the circumstance, but know that input likethis has spaces
will generate multiple exceptions.在
main()
旁边添加throws IOException
,然后Add
throws IOException
besidemain()
, then在java中获取输入非常简单,你所要做的就是:
It is very simple to get input in java, all you have to do is:
您可以使用 BufferedReader 获取这样的用户输入:
这就是应用它们的方式
因此,当用户在控制台中输入他的名字时,“字符串名称”将存储该信息。
如果它是您要存储的数字,代码将如下所示:
希望这有帮助!
You can get user input like this using a BufferedReader:
This is how you apply them
So when the user types in his name into the console, "String name" will store that information.
If it is a number you want to store, the code will look like this:
Hop this helps!
可以是这样的...
Can be something like this...
您可以使用
Scanner
获取用户输入。您可以使用正确的方法对不同的数据类型进行正确的输入验证,例如针对String
的next()
或针对Integer 的
。nextInt()
You can get the user input using
Scanner
. You can use the proper input validation using proper methods for different data types likenext()
forString
ornextInt()
forInteger
.这是使用 System.in.read() 函数的简单代码。这段代码只是写出输入的内容。如果您只想接受一次输入,则可以摆脱 while 循环,并且如果您愿意,可以将答案存储在字符数组中。
This is a simple code that uses the
System.in.read()
function. This code just writes out whatever was typed. You can get rid of the while loop if you just want to take input once, and you could store answers in a character array if you so choose.我喜欢以下内容:
例如,我会这样做:
I like the following:
and for example, I would do:
正如其他人所发布的那样,可以使用扫描仪进行键盘输入。但在这个高度图形化的时代,制作没有图形用户界面(GUI)的计算器是毫无意义的。
在现代 Java 中,这意味着使用 Scene Builder 等 JavaFX 拖放工具来放置输出一个类似于计算器控制台的 GUI。
请注意,使用 Scene Builder 直观上很简单,并且不需要您已有的事件处理程序的额外 Java 技能。
对于用户输入,GUI 控制台顶部应该有一个宽文本字段。
用户可以在此处输入他们想要执行功能的数字。
在文本字段下方,您将有一组功能按钮,用于执行基本功能(即加/减/乘/除和记忆/调用/清除)功能。
一旦设计好 GUI,您就可以添加“控制器”引用,将每个按钮功能链接到其 Java 实现,例如调用项目控制器类中的方法。
此视频有点旧,但仍然显示了场景生成器的使用方法是多么简单。
Keyboard entry using Scanner is possible, as others have posted. But in these highly graphic times it is pointless making a calculator without a graphical user interface (GUI).
In modern Java this means using a JavaFX drag-and-drop tool like Scene Builder to lay out a GUI that resembles a calculator's console.
Note that using Scene Builder is intuitively easy and demands no additional Java skill for its event handlers that what you already may have.
For user input, you should have a wide TextField at the top of the GUI console.
This is where the user enters the numbers that they want to perform functions on.
Below the TextField, you would have an array of function buttons doing basic (i.e. add/subtract/multiply/divide and memory/recall/clear) functions.
Once the GUI is lain out, you can then add the 'controller' references that link each button function to its Java implementation, e.g a call to method in your project's controller class.
This video is a bit old but still shows how easy Scene Builder is to use.
获取用户输入的最简单方法是使用 Scanner。下面是一个如何使用它的示例:
代码行
import java.util.Scanner;
告诉程序程序员将在其代码中使用用户输入。正如它所说,它导入了扫描仪实用程序。Scanner sc=new Scanner(System.in);
告诉程序启动用户输入。完成此操作后,您必须创建一个没有值的字符串或整数,然后将它们放入a=sc.nextInt();
或a=sc.nextLine();< 行中/代码>。这为变量提供了用户输入的值。然后您可以在您的代码中使用它。希望这有帮助。
The most simple way to get user input would be to use Scanner. Here's an example of how it's supposed to be used:
The line of code
import java.util.Scanner;
tells the program that the programmer will be using user inputs in their code. Like it says, it imports the scanner utility.Scanner sc=new Scanner(System.in);
tells the program to start the user inputs. After you do that, you must make a string or integer without a value, then put those in the linea=sc.nextInt();
ora=sc.nextLine();
. This gives the variables the value of the user inputs. Then you can use it in your code. Hope this helps.使用 JOptionPane 你可以实现它。
Using JOptionPane you can achieve it.