gpt4 book ai didi

Java 扫描器类 - 基础

转载 作者:行者123 更新时间:2023-12-01 11:57:20 28 4
gpt4 key购买 nike

我试图让我的用户通过使用扫描仪类来决定要做什么,但我有一个问题,代码一旦运行就不会激活,并且它不会让我跳过任何行。我的代码如下所示:

    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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com