[英]I'm having an issue with my game
所以我正在制作一個基本游戲,但我遇到的問題是第二場戰斗不會開始,第一場戰斗確實進行得很好,但是由於某種原因第二場戰斗不會開始...
這是代碼:
import java.util.Scanner;
公共類游戲{
public int Health = 10;
public int Enemy_Health = 25;
public int Your_Health = 20;
public int Battle = 1;
public int Move = 0;
public void Method() {
System.out.println("A random battle started!" + "\n");
System.out.println("It's health is currently at " + Health + "\n");
System.out.println("Your health is currently at " + Your_Health + "\n");
System.out.println("Would you like to use bite or slash?" + "\n");
//-----Battle 1-----\\
while (Battle == 1) {
if (Battle == 1) {
Scanner Scan = new Scanner(System.in);
String a = Scan.nextLine();
if (a.equals("bite")) {
System.out.println("You used bite!" + "\n");
System.out.println("You did 5 damage!" + "\n");
Health -= 5;
Move++;
System.out.println("It's health is now at " + Health + "\n");
} else if (a.equals("slash")) {
System.out.println("You used slash!" + "\n");
System.out.println("You did 2 Damage!" + "\n");
Health -= 2;
System.out.println("It's health is now at " + Health + "\n");
System.out.println("You did 3 Damage!" + "\n");
Health -= 3;
Move ++;
System.out.println("It's health is now at " + Health + "\n");
}
if (Move == 1) {
System.out.println("---------------------------" + "\n");
System.out.println("It used Bite" + "\n");
System.out.println("It did 5 damage!" + "\n");
Your_Health -= 5;
System.out.println("Your health is now at " + Your_Health + "\n");
System.out.println("---------------------------" + "\n");
System.out.println("Would you like to use bite or slash?" + "\n");
}
if (Health == 0) {
System.out.println("You won!" + "\n");
System.out.println("You gained 100$!" + "\n" + "You now have 150$!");
System.out.println("You gained 100 Xp!" + "\n" + "You leveled up!" + "\n" + "50 Xp left tell Level 2" + "\n" + "New move learned: " + "\n" +
"Knock Out!" + "\n" + "Your health went up by 5!" + "\n");
System.out.println("Swirl will make the enemy dizzy for one round!" + "\n" + "\n");
Your_Health += 5;
Battle -= 0;
Move = 0;
Health = -1;
}
while (Battle == 0){
Battle = 2;
}
//-----Battle 2-----\\
while (Battle == 2) {
if (Enemy_Health == 25) {
System.out.println("A random battle has started!" + "\n");
System.out.println("It's health is currently at " + Enemy_Health + "\n");
System.out.println("Your health is currently at " + Your_Health + "\n");
System.out.println("Would you like to use bite, slash or swirl?" + "\n");
if (a.equals("bite")) {
System.out.println("You used bite!" + "\n");
System.out.println("You did 5 damage!" + "\n");
Health -= 5;
System.out.println("It's health is now at " + Health + "\n");
} else if (a.equals("slash")) {
System.out.println("You used slash!" + "\n");
System.out.println("You did 2 Damage!" + "\n");
Health -= 2;
Move++;
System.out.println("It's health is now at " + Health + "\n");
System.out.println("You did 3 Damage!" + "\n");
Health -= 3;
Move++;
System.out.println("It's health is now at " + Health + "\n");
} else if (a.equals("swirl")) {
System.out.println("You used Swirl" + "\n");
System.out.println("It got dizzy for one round." + "\n");
}
if (Move == 1) {
System.out.println("---------------------------" + "\n");
System.out.println("It used Slash" + "\n" + "It did 2 damage!" + "\n" + "It did 3 damage!" + "\n");
Your_Health -= 5;
System.out.println("Your health is now at " + Your_Health + "\n");
System.out.println("---------------------------" + "\n");
System.out.println("Would you like to use bite or slash?" + "\n");
}
if (Move == 2){
System.out.println("---------------------------" + "\n");
System.out.println("It used Bite" + "\n" + "It did 5 damage!" + "\n");
Your_Health -= 5;
System.out.println("Your health is now at " + Your_Health + "\n");
System.out.println("---------------------------" + "\n");
System.out.println("Would you like to use bite or slash?" + "\n");
}
if (Move == 3){
System.out.println("---------------------------" + "\n");
System.out.println("It used Head Butt" + "\n" + "It did 10 damage!" + "\n" + "It took 5 damage" + "\n");
Your_Health -= 10;
Enemy_Health -= 5;
System.out.println("Your health is now at " + Your_Health + "\n");
System.out.println("---------------------------" + "\n");
System.out.println("Would you like to use bite or slash?" + "\n");
}
}
}
}
}
}
}
在第一次運行while循環后,運行狀況將小於10,因此if塊不再運行。 由於我看不到任何好處,因此建議您刪除if(Health == 10)塊。
就像評論中提到的那樣: 健康與戰斗應保持不浮動。
在解決了其他問題之后,您會注意到戰斗永遠不會結束,而且您永遠也不會咬人而獲勝:獲勝條件的檢查必須在其他if之外,並將Battle設置為某事!= 1
總的來說,您的游戲應該看起來像這樣:
public class Game {
public int Health = 10;
public int Battle = 1;
public void Method() {
System.out.println("A random battle started!" + "\n");
System.out.println("It's health is currently at " + Health + "\n");
System.out.println("Would you like to use bite or slash?" + "\n");
while (Battle == 1) {
Scanner Scan = new Scanner(System.in);
String a = Scan.nextLine();
if (a.equals("bite")) {
System.out.println("You used bite!"+ "\n");
System.out.println("You did 5 damage!"+ "\n");
Health -= 5;
System.out.println("It's health is now at " + Health + "\n");
System.out.println("Would you like to use bite or slash?"+ "\n");
} else if (a.equals("slash")) {
System.out.println("You used slash!"+ "\n");
System.out.println("You did 2 Damage!"+ "\n");
Health -= 2;
System.out.println("It's health is now at " + Health+ "\n");
System.out.println("You did 2 Damage!"+ "\n");
Health -= 2;
System.out.println("It's health is now at " + Health+ "\n");
System.out.println("Would you like to use bite or slash?"+ "\n");
}
if (Health <= 0) {
System.out.println("You won!"+ "\n");
System.out.println("You gained 100$!" + "\n" + "You now have 150$!");
Battle = 0;
}
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.