Java 无限做while循环?
我想做一个循环,每次答案不是1、2、3或4时都会打印出错误的答案。。。。 我希望它无限运行,我的不检测答案是对是错,它只是打印出无效的答案,然后再次询问,然后崩溃!我不知道为什么会这样 这是我的代码不要读“文本”部分,因为它是法语的,只要看看代码就行了Java 无限做while循环?,java,loops,Java,Loops,我想做一个循环,每次答案不是1、2、3或4时都会打印出错误的答案。。。。 我希望它无限运行,我的不检测答案是对是错,它只是打印出无效的答案,然后再次询问,然后崩溃!我不知道为什么会这样 这是我的代码不要读“文本”部分,因为它是法语的,只要看看代码就行了 System.out.println("Veuillez Choisir 1 des 4 groupes alimentaires suivants: (1)Légumes et fruits , (2) Produit cérealiers ,
System.out.println("Veuillez Choisir 1 des 4 groupes alimentaires suivants: (1)Légumes et fruits , (2) Produit cérealiers , (3) Laits et Substitues , (4) Viandes et substitues :");
System.out.println("\n");
answer = Clavier.lireIntLn();
do {
System.out.println("Votre choix est invalide");
System.out.println("Veuillez Choisir 1 des 4 groupes alimentaires suivants: (1)Légumes et fruits , (2) Produit cérealiers , (3) Laits et Substitues , (4) Viandes et substitues :");
answer = Clavier.lireIntLn();
continue;
}while (answer<1 && answer>5);
System.out.println(“Veuillez Choisir 1 des 4组食品添加剂:(1)树胶和水果,(2)产品添加剂,(3)Laits和取代基,(4)Viandes和取代基:”;
System.out.println(“\n”);
答案=Clavier.lireIntLn();
做{
系统输出打印(“Votre choix est invalide”);
系统输出打印(“Veuillez Choisir 1 des 4组食品添加剂:(1)树胶和水果,(2)产品制造商,(3)Laits和取代基,(4)Viandes和取代基:”;
答案=Clavier.lireIntLn();
继续;
}而(答5);
continue语句导致程序执行立即跳回do
,甚至不计算while条件。这意味着永远不会对条件求值,循环将变得无限大
删除continue,当while条件为false时,将保留循环。不幸的是,它总是错误的,因为值不能低于1和高于5。如果要继续,直到用户输入5,请尝试
while (answer != 5)
当您想要(如书面所示)允许除1、2、3或4之外的任何答案时,请执行此操作
while(answer >= 1 && answer <= 4);
while(answer>=1&&answer语句导致程序执行立即跳回do
,甚至不计算while条件。这意味着永远不会计算该条件,循环变得无限
删除continue,当while条件为false时,循环将被保留。不幸的是,它将始终为false,因为值不能低于1和高于5。如果要继续,直到用户输入5,请重试
while (answer != 5)
当您想要(如书面所示)允许除1、2、3或4之外的任何答案时,请执行此操作
while(answer >= 1 && answer <= 4);
while(answer>=1&&answer代码更像这样:
do
{
System.out.println("Veuillez Choisir 1 des 4 groupes alimentaires suivants: (1)Légumes et fruits , (2) Produit cérealiers , (3) Laits et Substitues , (4) Viandes et substitues :");
System.out.println("\n");
answer = Clavier.lireIntLn();
if( answer < 1 || answer > 4 )
{
//print your error message here
}
}while(true)
do
{
系统输出打印(“Veuillez Choisir 1 des 4组食品添加剂:(1)树胶和水果,(2)产品制造商,(3)Laits和取代基,(4)Viandes和取代基:”;
System.out.println(“\n”);
答案=Clavier.lireIntLn();
如果(答案<1 | |答案>4)
{
//在此处打印错误消息
}
}while(true)
代码更像这样:
do
{
System.out.println("Veuillez Choisir 1 des 4 groupes alimentaires suivants: (1)Légumes et fruits , (2) Produit cérealiers , (3) Laits et Substitues , (4) Viandes et substitues :");
System.out.println("\n");
answer = Clavier.lireIntLn();
if( answer < 1 || answer > 4 )
{
//print your error message here
}
}while(true)
do
{
系统输出打印(“Veuillez Choisir 1 des 4组食品添加剂:(1)树胶和水果,(2)产品制造商,(3)Laits和取代基,(4)Viandes和取代基:”;
System.out.println(“\n”);
答案=Clavier.lireIntLn();
如果(答案<1 | |答案>4)
{
//在此处打印错误消息
}
}while(true)
continue阻止它到达while语句。如果删除它,它将进入while条件。continue阻止它到达while语句。如果删除它,它将进入while条件。首先需要删除continue
完成此操作后,您需要处理以下问题:
while (answer<1 && answer>5)
while(回答5)
这永远不会发生。您需要这样做:
while (answer<1 || answer>5)
while(回答5)
数字不能小于1而大于5。首先需要删除继续
完成此操作后,您需要处理以下问题:
while (answer<1 && answer>5)
while(回答5)
这永远不会发生。您需要这样做:
while (answer<1 || answer>5)
while(回答5)
你不能让一个数字小于1而大于5。删除继续
我不明白开发人员如何使用do而或继续
是可怕的事情。删除继续
我不明白开发人员如何使用do而或继续
是可怕的事情。“我希望它无限地运行”“我希望它无限地运行”