java输入字符的方法有哪些,附:java输入字符的解析与处理

The keyboard sends character data to the computer, even when the characters look like numbers. And the program sends characters to the monitor, even when it has calculated a numerical result. (Actually, characters are not sent directly to the monitor. They are sent to the graphics card which converts them into a video signal which is displayed on the monitor.)
键盘向计算机发送字符数据,即使字符看起来像数字 。程序向监视器发送字符,即使它已经计算了一个数值结果 。(实际上,字符不是直接发送到显示器的,而是发送到图形卡,图形卡将字符转换成视频信号,并显示在显示器上 。)
If your program does arithmetic, the input characters must be converted into one of the primitive numeric data types. This is done using a Scanner object. Then a result is calculated using arithmetic with the numeric data. The result must then converted into character data before it is sent to the monitor. This is done using a method of System.out.
如果程序执行算术运算,则必须将输入字符转换为一种基本的数字数据类型 。这是使用Scanner对象完成的 。然后,使用算术和数值数据计算结果 。然后,在将结果发送到监视器之前,必须将其转换为字符数据 。这是通过使用System.out来实现的 。

java输入字符的方法有哪些,附:java输入字符的解析与处理

文章插图
Here is a picture of a Java program doing character input and output. In this picture, the white box represents the entire program. The program has imported Scanner for use with input, and has imported System.out for use with output.
上面是一个Java程序进行字符输入和输出的图片 。在这张图中,白框代表整个程序 。该程序已导入Scanner用于输入,并已导入System.out用于输出 。
To use the methods of a Scanner an object must be constructed. (The diagram shows the object as a rectangle.)
要使用Scanner的方法,必须构造一个对象 。(该图将对象显示为矩形 。)
The nextLine() method of Scanner reads one line of character data from the keyboard. The characters go into a String object. An assignment statement puts a reference to the object in the reference variable inData. To output the characters to the monitor, the program uses the println() method of System.out.
1 nextLine() Scanner的nextLine()方法从键盘读取一行字符数据 。这些字符进入字符串对象 。赋值语句将对象的引用放入引用变量inData中 。为了将字符输出到监视器,程序使用System.out的println()方法 。
java输入字符的方法有哪些,附:java输入字符的解析与处理

文章插图
2 nextInt() The nextInt() method of a Scanner object reads in a string of digits (characters) and converts them into an int type.
Scanner对象的nextInt()方法读取一串数字(字符),并将其转换为int类型 。
The Scanner object reads the characters one by one until it has collected those that are used for one integer. Then it converts them into a 32-bit numeric value. Usually that value is stored in an int variable.
Scanner对象逐个读取字符,直到收集了用于一个整数的字符 。然后将其转换为32位数值 。通常,该值存储在int变量中 。
The picture shows a program that reads in character data and then converts it into an integer which is stored in num. Next the program does arithmetic with num and stores the result in square. Finally the result is sent to println which converts the numeric result into characters and prints them out.
图中显示了一个程序,该程序读取字符数据,然后将其转换为存储在num中的整数 。接下来,该程序对num进行算术运算,并将结果存储为平方 。最后,结果被发送到println,println将数字结果转换为字符并打印出来 。
java输入字符的方法有哪些,附:java输入字符的解析与处理

文章插图
【java输入字符的方法有哪些,附:java输入字符的解析与处理】The nextInt() method scans through the input stream character by character, gathering characters into a group that can be converted into numeric data. It ignores spaces and end-of-lines that may preceed the group.
nextInt()方法逐个字符扫描输入流,将字符收集到可以转换为数字数据的组中 。它设计为忽略可能位于组前面的空格和行尾 。
A space or end-of-line charater that follows a string of digits ends the group. A non-digit character cannot be part of a group (not even at

    推荐阅读