[英]Are else statements not compatible with while loops?
我正在尝试对敌人的健康进行一次while循环,以便它不断询问要使用什么法术,直到敌人的健康小于或等于零为止,但是else语句似乎不起作用...
while(monster_base_health>0) {
System.out.println("Would you like to use 1. Fire Blast, 2. Ice Blast, 3. Void Ray? (Just type 1, 2, or 3)");
int attack;
attack = input.nextInt();
if(attack==1) {
monster_base_health = monster_base_health - mage_fire;
System.out.println("You used Fire Blast. The Monster now has " + monster_base_health + " health!");
}
if(attack==2) {
monster_base_health = monster_base_health - mage_iceblast;
System.out.println("You used Ice Blast. The monster has " + monster_base_health + " health!");
}
if(attack==3) {
monster_base_health = monster_base_health - mage_voidray;
System.out.println("You used Void Ray. The monster has " + monster_base_health + " health!");
}
} else {
System.out.println("You have defeated the monster!!!");
}
首先,对以下一般问题的答案是: 不, while
循环没有其他条件。
由于while
是运行多次(只要条件为真)的迭代器,因此您应该在while
循环内检查是否存在if
( else
(就像id一样)。 您可以通过以下方式检查特殊条件:
a = 10
while(a > 0) {
if(a == 1) {
echo "one";
} else if(a == 2) {
echo "two"
} else {
// Catch anything else
echo "more than two"
}
// decrease a by 1
a = a-1
}
// only when the while condition isn't true any more, the next statement is executed
echo "a is zero!"
注意:这并不是有效的编程语言,而是伪代码。
现在来看代码中的一般逻辑问题:
您不需要任何其他声明。 随着while
的执行直到monster_base_health
为零或更少,下面的下monster_base_health
语句仅在while
离开时才打印出来。
while(monster_base_health > 0) {
System.out.println("Would you like to use 1. Fire Blast, 2. Ice Blast, 3. Void Ray? (Just type 1, 2, or 3)");
int attack;
attack = input.nextInt();
if(attack == 1) {
monster_base_health = monster_base_health - mage_fire;
System.out.println("You used Fire Blast. The Monster now has " + monster_base_health + " health!");
}
if(attack == 2) {
monster_base_health = monster_base_health - mage_iceblast;
System.out.println("You used Ice Blast. The monster has " + monster_base_health + " health!");
}
if(attack == 3) {
monster_base_health = monster_base_health - mage_voidray;
System.out.println("You used Void Ray. The monster has " + monster_base_health + " health!");
}
}
System.out.println("You have defeated the monster!!!");
您期望else
语句做什么?
else
块与while
块无关。 它们与if
块有关。 语义概念是:
if (some condition is true)
do something
else (the condition was not true)
do something else
您想做的是这样的:
while (some condition is true)
do something
else (the condition was not true)
do something else
这实际上没有多大意义。 循环并不关心条件是否成立,它只在条件成立时执行。 (可能是从零到无限执行循环的任何地方。)
这样想吧...如果在条件为真时循环继续进行,则循环一旦完成就根据定义不再为真。 因此,循环后的所有内容都表示“条件不成立”。 因此,您可以执行以下操作:
while (the condition is true)
do something
do the next thing
无需else
阻止。
其他答案向您展示了如何使用if
语句以及if
和else
如何彼此关联。
这是实现您想要的另一种方法。 您可以使用switch
语句代替if/else
来使代码更简洁。 请注意,因为您的循环条件会测试monster_base_health
是否为0,所以一旦您monster_base_health
循环,就意味着monster_base_health
达到0,因此您可以在此处打印语句。
while (monster_base_health > 0) {
System.out
.println("Would you like to use 1. Fire Blast, 2. Ice Blast, 3. Void Ray? (Just type 1, 2, or 3)");
int attack = input.nextInt();
switch (attack) {
case 1:
monster_base_health = monster_base_health - mage_fire;
System.out.println("You used Fire Blast. The Monster now has "
+ monster_base_health + " health!");
break;
case 2:
monster_base_health = monster_base_health - mage_iceblast;
System.out.println("You used Ice Blast. The monster has "
+ monster_base_health + " health!");
break;
case 3:
monster_base_health = monster_base_health - mage_voidray;
System.out.println("You used Void Ray. The monster has "
+ monster_base_health + " health!");
break;
default:
System.out.println("Please use a valid attack");
break;
}
}
System.out.println("You have defeated the monster!!!");
注意1 :使用default
情况下,涵盖根据您的程序逻辑被视为无效的所有选项。 这意味着,如果用户输入的攻击值不是1、2或3,则将执行default
情况,告诉他们使用有效攻击。
注2:不要忘记break
了各自的case
。 如果您不这样做,那么下case
也将被执行,无论其条件是否发生。 然后是该语句的下一个,然后是该下一个语句的下一个,依此类推,直到遇到break
或}
到switch
块末尾为止。
其他块是if-else条件语句的一部分,它们不是条件循环结构的一部分。
您是不是要让else成为while循环中if块的一部分? 如果是这样,只需将else块与应该配对的if块串联即可。
这是一个基于您正在谈论的代码的示例:
while(monster_base_health>0)
{
System.out.println("Would you like to use 1. Fire Blast, 2. Ice Blast, 3. Void Ray? (Just type 1, 2, or 3)");
int attack;
attack = input.nextInt();
if(attack==1)
{
monster_base_health = monster_base_health - mage_fire;
System.out.println("You used Fire Blast. The Monster now has " + monster_base_health + " health!");
} else if(attack==2)
{
monster_base_health = monster_base_health - mage_iceblast;
System.out.println("You used Ice Blast. The monster has " + monster_base_health + " health!");
} else if(attack==3)
{
monster_base_health = monster_base_health - mage_voidray;
System.out.println("You used Void Ray. The monster has " + monster_base_health + " health!");
}
} // end while
System.out.println("You have defeated the monster!!!");
当输入是除3以外的任何其他值(1,2,4,5,....)时,您可能会进入else节。
您必须使用else if而不是plain if 。
if(xx==1){
}else if(xx==2){
}else if(xx==3){
}else{
}
while循环没有什么可处理的
或者您可能正在尝试这样的事情
while(h>0){
if(xx==1){
h-=1;
}else if(xx==2){
h-=2;
}else if(xx==3){
h-=3;
}else{
// wrong input- do nothing
}
} // end of while
// reached here means h is 0 or less
display -> you are dead //dead code here
不需要其他。 当它不满足循环条件时,它将退出循环,只需将您应该放置在循环之后的else中放置您的代码即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.