Java 有一些循环问题的小程序
只是有一个小问题,我正在为入门级编程课程做一个小程序 这个循环是无限重复的,我似乎不明白为什么。我怀疑do循环中的while循环存在某种冲突,这导致循环不断重复。 代码如下: 导入java.util.* 公开级秘书长 {Java 有一些循环问题的小程序,java,loops,while-loop,do-while,infinite,Java,Loops,While Loop,Do While,Infinite,只是有一个小问题,我正在为入门级编程课程做一个小程序 这个循环是无限重复的,我似乎不明白为什么。我怀疑do循环中的while循环存在某种冲突,这导致循环不断重复。 代码如下: 导入java.util.* 公开级秘书长 { }因为您需要再次要求用户输入新的猜测,您只需从用户处获取一次输入,如果相等停止将结束程序,否则将永远循环,请尝试以下操作: while (!input.equals("stop")) //If the user doesn't want to stop, continue
}因为您需要再次要求用户输入新的
猜测
,您只需从用户处获取一次输入,如果相等停止
将结束程序,否则将永远循环,请尝试以下操作:
while (!input.equals("stop")) //If the user doesn't want to stop, continue
{
if (input.contains("" + answer)) //If the input contains the answer, print the following statement
{
System.out.println("Your input contained the secret letter");
} else //If the input doesn't contain the answer, print the following statement
{
System.out.println("Your input does not contain the secret letter");
}
System.out.println("What is your guess?");
input = sc.next();///////////////here the user will enter the new guess
}
因为您需要再次要求用户输入新的
guess
,您只需从用户处获取一次输入,如果equalstop
将结束程序,如果不是,将永远循环,请尝试以下操作:
while (!input.equals("stop")) //If the user doesn't want to stop, continue
{
if (input.contains("" + answer)) //If the input contains the answer, print the following statement
{
System.out.println("Your input contained the secret letter");
} else //If the input doesn't contain the answer, print the following statement
{
System.out.println("Your input does not contain the secret letter");
}
System.out.println("What is your guess?");
input = sc.next();///////////////here the user will enter the new guess
}
看看你的内环。你不能在那里重新分配输入 看看你的内环。你不能在那里重新分配输入 不需要两个循环。外部Do..while循环就足够了。DO循环执行一次,然后再次扫描新令牌,然后基于此决定下一次迭代 但是,首先,如果您不从键盘输入“stop”,则内部循环的条件将为真 有两种选择:
input=sc.next()代码>在内部while循环中,因此条件有可能为False并退出循环
input = sc.next();
或者完全移除内部循环。仅保留外部while循环,当您继续从控制台输入字符串时,它将继续迭代,并在您键入“stop”
时停止
"stop"
我会选择第二个选项。尽管在第一个选项中,要停止两个循环,您必须键入stop两次,一次用于内部循环,一次用于外部循环
只需精确地缩进和编辑代码,在下一行缩进时不要使用空格,使用制表符,这会使代码变得优雅
您可以使用答案高于我答案的人给出的代码。不需要两个循环。外部Do..while循环就足够了。DO循环执行一次,然后再次扫描新令牌,然后基于此决定下一次迭代
但是,首先,如果您不从键盘输入“stop”,则内部循环的条件将为真
有两种选择:
要么添加另一个input=sc.next()代码>在内部while循环中,因此条件有可能为False并退出循环
input = sc.next();
或者完全移除内部循环。仅保留外部while循环,当您继续从控制台输入字符串时,它将继续迭代,并在您键入“stop”
时停止
"stop"
我会选择第二个选项。尽管在第一个选项中,要停止两个循环,您必须键入stop两次,一次用于内部循环,一次用于外部循环
只需精确地缩进和编辑代码,在下一行缩进时不要使用空格,使用制表符,这会使代码变得优雅
您可以使用答案高于我答案的人给出的代码。您在do循环中不必要地使用了while循环
它应该是一个if语句,一切都将按照您的要求进行
do
{
System.out.println("What is your guess?");
input = sc.next();
System.out.println("input" + input);
if(!input.equals("stop")) //If the user doesn't want to stop, continue
{
if(input.contains(""+answer)) //If the input contains the answer, print the following statement
{
System.out.println("Your input contained the secret letter");
}
else //If the input doesn't contain the answer, print the following statement
{
System.out.println("Your input does not contain the secret letter");
}
}
}
while(!input.equals("stop")); //loop the program if the input is not equal to
'stop'
}
在do循环中不必要地使用while循环
它应该是一个if语句,一切都将按照您的要求进行
do
{
System.out.println("What is your guess?");
input = sc.next();
System.out.println("input" + input);
if(!input.equals("stop")) //If the user doesn't want to stop, continue
{
if(input.contains(""+answer)) //If the input contains the answer, print the following statement
{
System.out.println("Your input contained the secret letter");
}
else //If the input doesn't contain the answer, print the following statement
{
System.out.println("Your input does not contain the secret letter");
}
}
}
while(!input.equals("stop")); //loop the program if the input is not equal to
'stop'
}
你应该试着把它的格式设置得稍微好一点,这样缩进就排成一行了。它有助于提高可读性,有时也有助于解决问题。另外,为什么内部有一个while循环,其条件与do-while循环相同?你应该去掉嵌套的while循环。当我把它粘贴到这个网站上时,我的格式似乎总是有问题。我想这可能是我的浏览器出了问题。不管怎样,这是我又一个愚蠢的错误。谢谢大家的帮助。没问题,我们是来帮忙的!你应该试着把它的格式设置得稍微好一点,这样缩进就排成一行了。它有助于提高可读性,有时也有助于解决问题。另外,为什么内部有一个while循环,其条件与do-while循环相同?你应该去掉嵌套的while循环。当我把它粘贴到这个网站上时,我的格式似乎总是有问题。我想这可能是我的浏览器出了问题。不管怎样,这是我又一个愚蠢的错误。谢谢大家的帮助。没问题,我们是来帮忙的!