gpt4 book ai didi

java - 为什么我的程序循环次数超过需要的次数?

转载 作者:行者123 更新时间:2023-11-30 06:46:24 24 4
gpt4 key购买 nike

我对编程完全陌生,我正在为类制作一个程序。对于这个项目,我需要制作一个“ self 指导”的程序。我想做一个简单的程序;我想要一个格斗模拟器!所以程序的重点是这样做:介绍你,然后开始战斗。攻击完全是随机的,一共 5 次,你和“敌人”战斗直到你们中的一个 hp 为 0。我希望程序结束,并显示你和你的敌人的生命值,并告诉你是否赢了。非常简单,但它比你想象的要难,尤其是在我缺乏编程知识的情况下。我只需要帮助修复它或简化它。感谢您提供的信息,如果您能提供帮助!代码如下:

import java.util.Scanner;

public class TG_UN4EX13 {

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);

String rumble = "startvalue";

System.out.println("Welcome to the Ultimate Battle Game!");
System.out.println("Have Fun!");

// Attack Section //
int attack1 = 5;
int attack2 = 10;
int attack3 = 20;
int attack4 = 40;
int cower = 0;

// Character Section//
int playerhealth = 100;
// Character Section//

// Enemy Section//
int enemyhealth = 100;
// Enemy Section//

while (playerhealth >= 0 || enemyhealth >= 0 || rumble.equalsIgnoreCase("Yes")) {
int attacknumber = (int) (Math.random() * (5 - 1 + 1) + 1);
int attacknumber2 = (int) (Math.random() * (5 - 1 + 1) + 1);

if (attacknumber == 1) {
enemyhealth = enemyhealth - attack1;
System.out.println("You used Punch!");
System.out.println("You did " + attack1 + " damage! \n");
} else if (attacknumber == 2) {
enemyhealth = enemyhealth - attack2;
System.out.println("You used Kick!");
System.out.println("You did " + attack2 + " damage! \n");
} else if (attacknumber == 3) {
enemyhealth = enemyhealth - attack3;
System.out.println("You used Jab!");
System.out.println("You did" + attack3 + " damage! \n");
} else if (attacknumber == 4) {
enemyhealth = enemyhealth - attack4;
System.out.println("You used Roundhouse Kick!");
System.out.println("You did " + attack4 + " damage! \n");
} else if (attacknumber == 5) {
enemyhealth = enemyhealth - cower;
System.out.println("You cowered in fear!");
System.out.println("You did " + cower + " damage! \n");
}

if (attacknumber2 == 1) {
playerhealth = playerhealth - attack1;
System.out.println("They used Punch!");
System.out.println("They did " + attack1 + " damage! \n");
} else if (attacknumber2 == 2) {
playerhealth = playerhealth - attack2;
System.out.println("They used Kick!");
System.out.println("They did " + attack2 + " damage! \n");
} else if (attacknumber2 == 3) {
playerhealth = playerhealth - attack3;
System.out.println("They used Jab!");
System.out.println("They did " + attack3 + " damage! \n");
} else if (attacknumber2 == 4) {
playerhealth = playerhealth - attack4;
System.out.println("They used Roundhouse Kick!");
System.out.println("They did " + attack4 + " damage! \n");
} else if (attacknumber2 == 5) {
playerhealth = playerhealth - cower;
System.out.println("They cowered in fear!");
System.out.println("They did " + cower + " damage! \n");
}

System.out.println("You have " + playerhealth + " health!");
System.out.println("They have " + enemyhealth + " health! \n");

}

if (playerhealth > enemyhealth) {
System.out.println("You won!");
System.out.println("Do you want to play again?");
rumble = scan.nextLine();
} else {
System.out.println("You lost!");
System.out.println("Do you want to play again?");
rumble = scan.nextLine();
}

if (rumble.equalsIgnoreCase("no")) {
System.out.println("Well, goodbye!");
System.exit(0);
}

}
}

附言这是一个快速编辑,但这是一个示例:

You have 20 health! They have 20 health!

You used Jab! You did20 damage!

They used Roundhouse Kick! They did 40 damage!

You have -20 health! They have 0 health!

You used Jab! You did20 damage!

They used Roundhouse Kick! They did 40 damage!

You have -60 health! They have -20 health!

You lost! Do you want to play again?

最佳答案

我不是很肯定,但我认为你可能需要改变:

while(playerhealth>=0 || enemyhealth>=0 || rumble.equalsIgnoreCase("Yes"))

while((playerhealth>0 && enemyhealth>0) || rumble.equalsIgnoreCase("Yes"))
如果健康度为 =0,它将再次运行,因此也删除 =

关于java - 为什么我的程序循环次数超过需要的次数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47561514/

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