Java 将整数调用为布尔值
代码中不起作用的部分Java 将整数调用为布尔值,java,while-loop,integer,boolean,Java,While Loop,Integer,Boolean,代码中不起作用的部分 我需要将整数调用到该部分。此代码是我为编程类创建的游戏的主要部分,如有任何帮助,将不胜感激。您需要“大于或等于”运算符:= if (bossHP >= 0){ System.out.println("Congratulations you won!"); break; } if (HP >= 0){ System.out.println("Sorry you lost."); break; } 好的,所以我不确定到底应该做什么
我需要将整数调用到该部分。此代码是我为编程类创建的游戏的主要部分,如有任何帮助,将不胜感激。您需要“大于或等于”运算符:
=
if (bossHP >= 0){
System.out.println("Congratulations you won!");
break;
}
if (HP >= 0){
System.out.println("Sorry you lost.");
break;
}
好的,所以我不确定到底应该做什么,但我会尝试一下。看起来你有点糊涂了 例如:
if (selection == 1){
waits();
System.out.println("You attack.");
System.out.println(bossOneHP == bossOneHP - (OP - bossOneBP));
}else if(selection == 2){
在这段代码中,您似乎在打印以控制变量bossOneHP和bossOneHP-(OP-bossOneBP)的比较,我认为这不是您想要的,因为只有当(OP-bossOneBP)为零(基本代数)时,语句才会打印为真。我怀疑你的意图是:
if (selection == 1){
waits();
System.out.println("You attack.");
bossOneHP = bossOneHP - (OP - bossOneBP);
}else if(selection == 2){
这将变量bossOneHP设置为自身负(OP减去bossOneBP)。注意:您也可以这样做:
if (selection == 1){
waits();
System.out.println("You attack.");
bossOneHP-= OP - bossOneBP;
}else if(selection == 2){
哪个更快将一个值设置为自身减去以下值,而不是将其设置为新值的=。此外,如果它们相等,则==进行比较并返回true,而=将变量设置为新值
第二期:
if (bossHP == (0 || >0){
我假设如果bossHP小于或等于零,您希望激活if语句。| |语句是一个布尔运算符(它比较任意一侧的两个布尔输入,无论是变量、比较还是函数,如果其中一个输入为true,则返回单个布尔值true),其功能与单词或类似。要比较两个数字(在本例中为变量BOSHP和零),请使用几个运算符之一。详情如下:
== -returns true (which activates the if statement) if the numbers or objects (if they are the same instance, not if they contain equal values) on both sides are identical.
< -returns true if the left hand number is smaller than the right hand one (doesn't work on objects)
> -returns true if the right hand number is smaller
<= -returns true if the left hand number is smaller or equal to the right hand number
>= -returns true if the right hand number is smaller or equal to the left hand number
!= -returns true if the numbers or objects do not equal each other (effective opposite of the == token)
! -only takes one boolean on the right hand side and returns its opposite (this inverts the value essentially), if(!val) is equivalent and better to if(val == false)
你也可以做while(真的)和break(休息)来代替while(继续播放)的事情;如果(boss>=0)输入3时的命令{
是您需要的。有关比较运算符和相等运算符的详细信息,请参阅“要调用到节的整数”是什么意思?
== -returns true (which activates the if statement) if the numbers or objects (if they are the same instance, not if they contain equal values) on both sides are identical.
< -returns true if the left hand number is smaller than the right hand one (doesn't work on objects)
> -returns true if the right hand number is smaller
<= -returns true if the left hand number is smaller or equal to the right hand number
>= -returns true if the right hand number is smaller or equal to the left hand number
!= -returns true if the numbers or objects do not equal each other (effective opposite of the == token)
! -only takes one boolean on the right hand side and returns its opposite (this inverts the value essentially), if(!val) is equivalent and better to if(val == false)
if (bossHP >= 0){
System.out.println("Congratulations you won!");
break;
}