简体   繁体   English

同时执行循环

[英]do-while loop not working as it should

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". 编写一个循环,从标准输入中读取字符串,其中字符串为“ land”,“ air”或“ water”。 The loop terminates when "xxxxx" (five x characters) is read in. Other strings are ignored. 当读入“ xxxxx”(五个x字符)时,循环终止。忽略其他字符串。 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:”组成,其后是读入的“ 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);

You are storing user info in stdInput but your while checks stdin . 您正在将用户信息存储在stdInput但同时检查了stdin Try this way 试试这个

String stdInput = null;

do {
    stdInput = stdin.next();

    //your ifs....

} while (!stdInput.equalsIgnoreCase("xxxxx"));

This Works:) 这有效:)

I just submitted this code to codelab and it works just fine. 我只是将此代码提交给了代码实验室,并且效果很好。

Write a loop that reads strings from standard input where the string is either "land", "air", or "water". 编写一个循环,从标准输入中读取字符串,其中字符串为“ land”,“ air”或“ water”。 The loop terminates when "xxxxx" (five x characters ) is read in. Other strings are ignored. 当读入“ xxxxx”(五个x字符)时,循环终止。忽略其他字符串。 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:”组成,其后是读入的“ 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);

我认为您想要stdInput.equalsIgnoreCase("xxxxx") == false而不是stdin.equalsIgnoreCase("xxxxx") == false

You are right - the problem is where you indicated. 您说对了-问题出在您指出的地方。 The solution is to not read again from stdin: 解决的办法是不要再次从stdin中读取:

Also, you must declare the stdInput before the 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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM