为什么我的程序循环次数比需要的多两倍?

时间:2017-11-29 20:45:10

标签: java loops

我对编程完全陌生,我正在为课程制作一个程序。对于这个项目,我需要做一个自我指导的"程序。我想制作一个简单的程序;我想要一个格斗模拟器!所以程序的重点是这样做: 介绍你,开始战斗。攻击是完全随机的5,你和#34;敌人"战斗直到你们中的一个命中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);
        }

    }
}

P.S。这是一个快速编辑,但这是一个例子:

  

你有20个健康!他们有20个健康!

     你使用了Jab!你做了20次伤害!

     

他们使用了Roundhouse Kick!他们造成40点伤害!

     

你有-20健康!他们有0健康!

     你使用了Jab!你做了20次伤害!

     

他们使用了Roundhouse Kick!他们造成40点伤害!

     

你有-60健康!他们有-20健康!

     你失败了!你想再玩一次吗?

2 个答案:

答案 0 :(得分:3)

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

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

while((playerhealth>0 && enemyhealth>0) || rumble.equalsIgnoreCase("Yes")) 如果运行状况为=0,则会再次运行,因此请同时删除=

答案 1 :(得分:1)

看一下循环的条件。

while (playerhealth >= 0 || enemyhealth >= 0 || rumble.equalsIgnoreCase("Yes")) {
    //do stuff
}

你说,“在任何以下条件为真的情况下继续战斗”:

  1. 玩家健康状况良好

  2. 敌人的健康是积极的

  3. Rumble = Yes

  4. 现在想想退出该循环将会采取什么措施(即结束游戏)。

    退出循环与停留在循环中相反,所以你想要与这些条件相反,即“当所有以下各项

    时停止战斗”:

    1. 玩家健康状况低于0

    2. 敌人的健康状况低于0

    3. Rumble!=是

    4. 你的意思是战斗结束只有当两个玩家和敌人都有负面的健康时,或者当 玩家或敌人的健康状况不佳时结束?