我试图让我的用户通过使用扫描仪类来决定要做什么,但我有一个问题,代码一旦运行就不会激活,并且它不会让我跳过任何行。我的代码如下所示:
Scanner input = new Scanner(System.in);
while (input.hasNextLine()){
if (input.findInLine("setFirst") != null){
System.out.println("setFirst");
}else if(input.findInLine("setSecond") != null){
System.out.println("setSecond");
}else if (input.findInLine("setOption") != null){
System.out.println("Please type in option.");
}
}
}
我认为 hasNextLine()
将确保我可以编写任意数量的行并且它将被视为输入?
最佳答案
如果我正确理解了您的意思,您可以尝试将代码更改为:
Scanner input = new Scanner(System.in);
String str;
while ((str = input.nextLine()) != null){
if (str.equals("setFirst")){
System.out.println("setFirst");
}else if(str.equals("setSecond")){
System.out.println("setSecond");
} else if (str.equals("setOption")){
System.out.println("Please type in option.");
}
}
关于Java 扫描器类 - 基础,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28343072/