gpt4 book ai didi

java - 我的游戏有问题

转载 作者:行者123 更新时间:2023-12-01 10:54:19 25 4
gpt4 key购买 nike

所以我正在制作一款基本游戏,我遇到了一个问题,即第二场战斗无法开始,第一场战斗运行得非常好,但由于某种原因第二场战斗无法开始......

代码如下:

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 block 不再运行。由于我没有看到它有任何好处,因此我建议您删除 if(Health==10) block 。

正如评论中提到的:生命值战斗应该是int而不是float。

解决其他问题后,您会发现战斗永远不会结束,并且您永远无法通过咬来获胜:您对获胜条件的检查必须在其他 if 之外并设置为某事而战!= 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;
}
}
}
}
}

关于java - 我的游戏有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33711781/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com