Java else语句是否与while循环不兼容?
我试图对敌人的生命值做一个while循环,这样它会不断询问使用什么法术,直到敌人的生命值小于或等于零,但else语句似乎不起作用Java else语句是否与while循环不兼容?,java,Java,我试图对敌人的生命值做一个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.ne
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!!!");
}
Else块是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!!!");
Else块是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!!!");
你希望这个
语句还能做什么
else
块与而块无关。它们与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
块。您希望此else
语句做什么
else
块与而块无关。它们与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
块。当输入为除3以外的任何其他值时(对于1,2,4,5,…),您可能进入else部分
必须使用else if而不是普通if
if(xx==1){
}else if(xx==2){
}else if(xx==3){
}else{
}
循环时没有什么可处理的
或者你可能正在尝试这样的事情
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
当输入为除3以外的任何其他值(对于1,2,4,5,…)时,您可能会进入else部分
必须使用else if而不是普通if
if(xx==1){
}else if(xx==2){
}else if(xx==3){
}else{
}
循环时没有什么可处理的
或者你可能正在尝试这样的事情
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
首先,一般问题的答案是:否,while
循环没有任何其他条件。
由于while
是一个多次运行的迭代器(只要条件为真),您应该检查while
循环中的if
和else
(正如您所做的那样)。您可以通过以下方式检查特殊情况:
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\u base\u 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!!!");
首先,一般问题的答案是:否,while
循环没有任何其他条件。
由于while
是一个多次运行的迭代器(只要条件为真),您应该检查while
循环中的if
和else
(正如您所做的那样)。您可以通过以下方式检查特殊情况:
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\u base\u 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!!!");
其他答案向您展示了应该如何使用if
语句,以及if
和else
如何相互关联
这是实现你想要的另一种方法。您可以使用switch
语句而不是if/else
,使代码更简洁。
请注意,由于循环条件测试monster\u base\u health
是否为0,因此一旦超出循环,就意味着monster\u base\u 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:使用默认值
案例涵盖根据程序逻辑被视为无效的所有选项。这意味着,如果用户输入除1、2或3之外的任何攻击值,则将执行默认值
案例,告知用户使用有效的攻击
注2:不要忘记从每个案例中分离。如果不这样做,那么下一个案例也将被执行,无论其条件是否发生。然后是该语句的下一个语句,然后是该语句的下一个语句,依此类推,直到遇到中断
,或者}
开关
块结束。<