Tay*_* P. 92 java java.util.scanner
尝试使用java.util.Scanner中的nextLine()方法时遇到了麻烦.
这是我尝试过的:
import java.util.Scanner;
class TestRevised {
public void menu() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a sentence:\t");
String sentence = scanner.nextLine();
System.out.print("Enter an index:\t");
int index = scanner.nextInt();
System.out.println("\nYour sentence:\t" + sentence);
System.out.println("Your index:\t" + index);
}
}
Run Code Online (Sandbox Code Playgroud)
示例#1:此示例按预期工作.String sentence = scanner.nextLine();在继续之前,该行等待输入输入System.out.print("Enter an index:\t");.
这会产生输出:
Enter a sentence: Hello.
Enter an index: 0
Your sentence: Hello.
Your index: 0
Run Code Online (Sandbox Code Playgroud)
// Example #2
import java.util.Scanner;
class Test {
public void menu() {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("\nMenu Options\n");
System.out.println("(1) - do this");
System.out.println("(2) - quit");
System.out.print("Please enter your selection:\t");
int selection = scanner.nextInt();
if (selection == 1) {
System.out.print("Enter a sentence:\t");
String sentence = scanner.nextLine();
System.out.print("Enter an index:\t");
int index = scanner.nextInt();
System.out.println("\nYour sentence:\t" + sentence);
System.out.println("Your index:\t" + index);
}
else if (selection == 2) {
break;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
示例#2:此示例无法按预期工作.此示例使用while循环和if - else结构允许用户选择要执行的操作.程序到达后String sentence = scanner.nextLine();,它不会等待输入,而是执行该行System.out.print("Enter an index:\t");.
这会产生输出:
Menu Options
(1) - do this
(2) - quit
Please enter your selection: 1
Enter a sentence: Enter an index:
Run Code Online (Sandbox Code Playgroud)
这使得无法输入句子.
为什么示例#2不能按预期工作?Ex之间的唯一区别.1和2是Ex.2有一个while循环和一个if-else结构.我不明白为什么这会影响scanner.nextInt()的行为.
Pet*_*rey 121
我认为你的问题是这样的
int selection = scanner.nextInt();
Run Code Online (Sandbox Code Playgroud)
只读取数字,而不是数字后面的行尾或任何内容.当你申报时
String sentence = scanner.nextLine();
Run Code Online (Sandbox Code Playgroud)
这将读取该行的剩余部分(在我怀疑的数字之后没有任何内容)
尝试放置scanner.nextLine(); 在每个nextInt()之后,如果你打算忽略该行的其余部分.
ber*_*rio 22
而不是scanner.nextLine()每次想要读取内容时都添加额外的内容,因为您似乎想要接受新行上的每个输入,您可能希望更改分隔符以实际仅匹配换行符(而不是任何空格,默认情况下) )
import java.util.Scanner;
class ScannerTest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
scanner.useDelimiter("\\n");
System.out.print("Enter an index: ");
int index = scanner.nextInt();
System.out.print("Enter a sentence: ");
String sentence = scanner.next();
System.out.println("\nYour sentence: " + sentence);
System.out.println("Your index: " + index);
}
}
Run Code Online (Sandbox Code Playgroud)
因此,要读取一行输入,您只需要scanner.next()具有下一个{Int,Double,...}的相同行为分隔符.
与"nextLine()每一次"的方式,所不同的是,后者将接受,作为指数也<space>3,3<space>和3<space>whatever而前者只接受3对自己的行
FAI*_*SAL 10
这是因为当你输入一个数字然后按Enter键时,input.nextInt()只消耗数字,而不是"行尾".像int,double等原始数据类型不消耗"行尾",因此"行尾"保留在缓冲区中,当input.next()执行时,它从第一个输入的缓冲区消耗"行尾".这就是为什么,你String sentence = scanner.next()只消耗"行尾"并且不等待从键盘读取.
提示:使用scanner.nextLine()而不是scanner.next()因为scanner.next()不从键盘读取空格.(在从键盘提供一些空格后截断字符串,仅在空格之前显示字符串.)
小智 9
不要尝试使用nextLine()扫描文本; 在使用相同扫描仪的nextInt()之后!它与Java Scanner不兼容,许多Java开发人员选择只使用另一个Scanner进行整数.如果需要,您可以将这些扫描仪称为scan1和scan2.
| 归档时间: |
|
| 查看次数: |
667102 次 |
| 最近记录: |