我的代码中没有工作的部分
}else if(bossOne == 3){
int bossOneHP = 70 + bossStat.nextInt(10)-5;
int bossOneOP = 27 + bossStat.nextInt(10)-5;
int bossOneBP = 12 + bossStat.nextInt(10)-5;
String bossName = "Spartan King";
waits();
System.out.println("Your first enemy is the Spartan King.");
System.out.println("It has a very powerful attack, lets hope you have enough health.");
}
boolean keepPlaying = true;
while (keepPlaying){
Scanner choice = new Scanner(System.in);
System.out.println("Enter 1 to attack.");
System.out.println("Enter 2 to block.");
System.out.println("Enter 3 to exit the game.");
int selection = choice.nextInt();
if (selection == 1){
waits();
System.out.println("You attack.");
System.out.println(bossOneHP == bossOneHP - (OP - bossOneBP));
}else if(selection == 2){
waits();
System.out.println("You block.");
System.out.println(HP == HP - (bossOneOP - BP));
}else if(selection == 3){
break;
}
if (bossHP == (0 || >0){
System.out.println("Congratulations you won!");
break;
}
if (HP == (0 || >0)){
System.out.println("Sorry you lost.");
break;
}
我需要将整数调用到该部分。这段代码是我为我的编程课创建的游戏的内容,任何帮助都会受到赞赏。
答案 0 :(得分:0)
您想要'大于或等于'运算符:>=
if (bossHP >= 0){
System.out.println("Congratulations you won!");
break;
}
if (HP >= 0){
System.out.println("Sorry you lost.");
break;
}
答案 1 :(得分:0)
好的,所以我不确定应该做什么,但我会尝试一下。看起来你有点困惑。
例如:
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){
我假设你想要激活if语句,如果bossHP小于或等于零。 || statement是一个布尔运算符(它比较两边的两个布尔输入,无论是变量还是比较或函数,如果任一输入为真,则返回单个布尔值true),并且不像单词那样起作用要么。要比较两个数字(在本例中为变量bossHP和零),您可以使用多个运算符之一。它们如下:
== -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;
}
也可以在执行while(keepPlaying)的同时执行(true)并执行break;输入3时的命令