带有 if else 语句的 Java Scanner 输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38802204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Scanner input with if else statement
提问by justsomerandomguy
Hi I'm new to java and trying to make a Quiz to practice. I wanna make a Question where the user has to combine words from to categories to pairs. Like A1 B4 C3 D2. What I did now is using an if else statement to check if the input is the correct answer, but it only works with 1A. For the others I can do 6 inputs, which is not what I want, and even if there's a correct one I don't get a point.
嗨,我是 Java 新手,正在尝试做一个测验来练习。我想提出一个问题,用户必须将单词从类别组合成对。像 A1 B4 C3 D2。我现在所做的是使用 if else 语句来检查输入是否为正确答案,但它仅适用于 1A。对于其他人,我可以做 6 个输入,这不是我想要的,即使有一个正确的输入,我也没有得到一分。
public class HelloWorld {
public static void main(String[] args) {
Scanner walther = new Scanner(System.in);
String cro = "1A";
String dan = "2C";
String fin = "4D";
String dut = "3F";
String fre = "5B";
String ger = "6E";
int x = 0;
if (cro.equalsIgnoreCase(walther.nextLine())){
++x;
walther.close();
}
else if (dan.equalsIgnoreCase(walther.nextLine())){
++x;
walther.close();
}
else if (fin.equalsIgnoreCase(walther.nextLine())){
++x;
walther.close();
}
else if (dut.equalsIgnoreCase(walther.nextLine())){
++x;
walther.close();
}
else if (fre.equalsIgnoreCase(walther.nextLine())){
++x;
walther.close();
}
else if (ger.equalsIgnoreCase(walther.nextLine())){
++x;
walther.close();
}
else {
walther.close();
}
System.out.println(x + " Point!");
}
}
采纳答案by Mureinik
Calling nextLine()
consumes a line from the scanner. You do this on the first if
, so the subsequent else if
branches are, in fact, comparing the following lines(or null
, if you don't have any additional input). Instead, you should consume the line only once, save it to a local variable and use that in your comparisons:
呼叫nextLine()
消耗来自扫描仪的线路。您在第一个 中执行此操作if
,因此后续else if
分支实际上是在比较以下几行(或者null
,如果您没有任何其他输入)。相反,您应该只使用该行一次,将其保存到局部变量并在比较中使用它:
String input = walther.nextLine();
if (cro.equlasIgnoreCase(input)) { // etc...
Having said that, using and if-else structure isn't the neatest solution. You can save a lot of code bloat by using a case insensitiveTreeSet
:
话虽如此, using 和 if-else 结构并不是最好的解决方案。您可以通过使用不区分大小写的方式来节省大量代码膨胀TreeSet
:
TreeSet<String> set = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
set.addAll(Arrays.asList("1A", "2C", "4D", "3F", "5B", "6E"));
String input = walther.nextLine();
if (set.contains(input)) {
++x;
walther.close();
}
回答by Shenshow Shodin
People the fact remains that the above provided answer is confusing especially to those who are relatively new to the java community. Hence the requirement is for an easier and simpler answer. Now, Java understands Strings only by the following code :
人们的事实仍然是,上面提供的答案令人困惑,尤其是对于那些对 Java 社区来说相对较新的人。因此,要求是一个更容易和更简单的答案。现在,Java 仅通过以下代码理解字符串:
Scanner sc=new Scanner(System.in);
String a=sc.next();
if(a.equals("xyzzy"))
{System.out.println("yes");
Scanner sc=new Scanner(System.in);
String a=sc.next();
if(a.equals("xyzzy"))
{System.out.println("yes");