簡體   English   中英

為什么我的程序循環次數超過所需次數兩次?

[英]Why is my program looping twice more than needed?

我是編程的新手,當時我正在上課。 對於該項目,我需要制作一個“自我指導”程序。 我想編寫一個簡單的程序; 我想要一個戰斗模擬器! 因此,該程序的目的是這樣做:介紹您,並開始戰斗。 攻擊完全是5隨機,然后您和“敵人”戰斗直到你們其中之一命中0 hp。 我希望該程序結束,並顯示您和您的敵人的健康狀況,並告訴您您是否獲勝。 非常簡單,但是它比您想象的要難,尤其是由於缺乏我學到的編程知識。 我只需要幫助修復或簡化它。 感謝您提供的信息! 代碼如下:

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);
        }

    }
}

PS這是一個快速編輯,但是這里有一個例子:

你有20點生命! 他們有20點生命!

你用過賈布! 你造成了20點傷害!

他們使用了Roundhouse Kick! 他們造成了40點傷害!

您的健康狀況為-20! 他們的健康為0!

你用過賈布! 你造成了20點傷害!

他們使用了Roundhouse Kick! 他們造成了40點傷害!

您有-60點生命! 他們有-20生命!

你輸了! 你想再玩一次嗎?

我不是很肯定,但我認為您可能需要更改:

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

while((playerhealth>0 && enemyhealth>0) || rumble.equalsIgnoreCase("Yes"))如果健康=0 ,它將再次運行,因此也應刪除=

看一下循環的條件。

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

您是在說:“在滿足以下任何條件的情況下繼續戰斗”:

  1. 球員健康是積極的

  2. 敵人的健康是積極的

  3. 隆隆聲=是

現在考慮退出該循環(即結束游戲)所需的時間。

退出循環與停留在循環中是相反的,因此您需要與這些條件相反,即,“在滿足以下所有條件時停止戰斗”:

  1. 玩家生命值小於0

  2. 敵人的健康狀況小於0

  3. 隆隆聲!=是

你的意思是為爭取到年底只有當兩個玩家和敵人造成不良的健康,或當任一玩家或敵人造成不良的健康應該結束?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM