java do-while 循环无法正常工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15015995/
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
do-while loop not working as it should
提问by Dragon Wolf
Ok so basically I am having trouble finding out why this is not working as I think it should, and need help getting to the right output. I have tried messing with this format a few ways, but nothing works, and I really don't understand why. Here are the instructions, followed by my source for it:
好吧,基本上我很难找出为什么这不能像我认为的那样工作,并且需要帮助才能获得正确的输出。我试过用几种方法来弄乱这种格式,但没有任何效果,我真的不明白为什么。这是说明,然后是我的来源:
INSTRUCTIONS
指示
Write a loop that reads strings from standard input where the string is either "land", "air", or "water". The loop terminates when "xxxxx" (five x characters) is read in. Other strings are ignored. After the loop, your code should print out 3 lines: the first consisting of the string "land:" followed by the number of "land" strings read in, the second consisting of the string "air:" followed by the number of "air" strings read in, and the third consisting of the string "water:" followed by the number of "water" strings read in. Each of these should be printed on a separate line.
编写一个循环,从标准输入中读取字符串,其中字符串是“land”、“air”或“water”。当读入“xxxxx”(五个 x 字符)时,循环终止。其他字符串将被忽略。在循环之后,您的代码应该打印出 3 行:第一行由字符串“land:”和读入的“land”字符串的数量组成,第二行由字符串“air:”和“的数量”组成air" 字符串读入,第三个由字符串 "water:" 组成,后跟读入的 "water" 字符串的数量。这些字符串中的每一个都应打印在单独的行上。
ASSUME the availability of a variable, stdin , that references a Scanner object associated with standard input.
假设变量 stdin 的可用性,该变量引用与标准输入关联的 Scanner 对象。
SOURCE:
来源:
int land = 0;
int air = 0;
int water = 0;
do
{
String stdInput = stdin.next();
if (stdInput.equalsIgnoreCase("land"))
{
land++;
}else if (stdInput.equalsIgnoreCase("air"))
{
air++;
}else if (stdInput.equalsIgnoreCase("water"))
{
water++;
}
}while (stdin.equalsIgnoreCase("xxxxx") == false); // I think the issue is here, I just dont't know why it doesn't work this way
System.out.println("land: " + land);
System.out.println("air: " + air);
System.out.println("water: " + water);
回答by Pshemo
You are storing user info in stdInput
but your while checks stdin
. Try this way
您正在存储用户信息,stdInput
但您的 while 检查stdin
。试试这个方法
String stdInput = null;
do {
stdInput = stdin.next();
//your ifs....
} while (!stdInput.equalsIgnoreCase("xxxxx"));
回答by HIGGO36
This Works:)
这有效:)
I just submitted this code to codelab and it works just fine.
我刚刚将此代码提交给了 codelab,它工作得很好。
Write a loop that reads strings from standard input where the string is either "land", "air", or "water". The loop terminates when "xxxxx" (five x characters ) is read in. Other strings are ignored. After the loop, your code should print out 3 lines: the first consisting of the string "land:" followed by the number of "land" strings read in, the second consisting of the string "air:" followed by the number of "air" strings read in, and the third consisting of the string "water:" followed by the number of "water" strings read in. Each of these should be printed on a separate line.
编写一个循环,从标准输入中读取字符串,其中字符串是“land”、“air”或“water”。当读入“xxxxx”(五个 x 字符)时,循环终止。其他字符串将被忽略。在循环之后,您的代码应该打印出 3 行:第一行由字符串“land:”和读入的“land”字符串的数量组成,第二行由字符串“air:”和“的数量”组成air" 字符串读入,第三个由字符串 "water:" 组成,后跟读入的 "water" 字符串的数量。这些字符串中的每一个都应打印在单独的行上。
int land = 0;
int air = 0;
int water = 0;
String word = "";
while(!(word.equals("xxxxx"))) {
word = stdin.next();
if(word.equals("land")) {
land++;
}else if(word.equals("air")) {
air++;
}else if(word.equals("water")) {
water++;
}
}
System.out.println("land:" + land);
System.out.println("air:" + air);
System.out.println("water:" + water);
回答by Eric Galluzzo
I think you want stdInput.equalsIgnoreCase("xxxxx") == false
instead of stdin.equalsIgnoreCase("xxxxx") == false
.
我认为你想要stdInput.equalsIgnoreCase("xxxxx") == false
而不是stdin.equalsIgnoreCase("xxxxx") == false
.
回答by Bohemian
You are right - the problem is where you indicated. The solution is to notread again from stdin:
你是对的 - 问题出在你指出的地方。解决方案是不再从标准输入读取:
Also, you must declare the stdInput
beforethe loop so its scope reaches the while condition:
此外,您必须在循环stdInput
之前声明,以便其范围达到 while 条件:
String stdInput = null;
do {
stdInput = stdin.next();
// rest of code the same
} while (!stdInput.equalsIgnoreCase("xxxxx"));
An alternate way would be a for loop:
另一种方法是 for 循环:
for (String stdInput = stdin.next(); !stdInput.equalsIgnoreCase("xxxxx"); stdInput = stdin.next()) {
// rest of code the same
}