循环不会停止

时间:2017-02-12 13:46:19

标签: java infinite-loop

我正在尝试为学校项目制作RPG。当你击败老板时,游戏结束。你可以在挑战老板之前训练和对抗随机怪物。我现在遇到的问题是战斗方法中的循环不会停止。

import java.util.Scanner;
import java.util.Random;

public class A{
public static void main(String []args){
    Scanner get = new Scanner(System.in);
    Random rng = new Random();

    int day = 0;
    int choice;
    String playerName;

    int playerHealth = 10; int playerAttack = 5; int playerDefense = 5;
    int boss1Health = 1000; int boss1Attack = 900; int boss1Defense = 900;
    int slimeHealth = 100; int slimeAttack = 20; int slimeDefense = 20;
    int goblinHealth = 80; int goblinAttack = 40; int goblinDefense = 10;

    System.out.print("What is your name? ");
    playerName = get.next();
    System.out.println("...Welcome, " + playerName);

    while(bothAreAlive(playerHealth, boss1Health)){
        ++day;
        while(true){
            action(day);
            choice = get.nextInt();
            if(choice == 1){
                fight(playerName, playerHealth, playerAttack, playerDefense, "Skeleton King", boss1Health, boss1Attack, boss1Defense);
                break;
            }
            if(choice == 2){
                int encounter = rng.nextInt(2);
                if(encounter == 0){
                    slimeHealth = 100;
                    fight(playerName, playerHealth, playerAttack, playerDefense, "Slime", slimeHealth, slimeAttack, slimeDefense);
                }
                else if(encounter == 1){
                    goblinHealth = 80;
                    fight(playerName, playerHealth, playerAttack, playerDefense, "Goblin", goblinHealth, goblinAttack, goblinDefense);
                }
                break;
            }
            if(choice == 3){
                playerHealth += 10;
                playerAttack += 5;
                playerDefense += 5;
                break;
            }
            if(choice == 4){
                showStats(playerName, playerHealth, playerAttack, playerDefense);
            }
        }
    }
}

public static int attack(String attacker, String defender, int aAttack, int dDefense, int dHealth){
    int damage = Math.max(0, aAttack - dDefense);
    System.out.println("\n" + attacker + " did " + damage + " damage to " + defender + ".");
    dHealth = dHealth - damage;
    System.out.println("\n" + defender + "'s health is now " + dHealth);
    return dHealth;
}

public static int fight(String playerName, int playerHealth, int playerAttack, int playerDefense, String enemyName, int enemyHealth, int enemyAttack, int enemyDefense){
    Random rng = new Random();
    while(bothAreAlive(playerHealth, enemyHealth)){
        int playerChance = rng.nextInt(5);
        int enemyChance = rng.nextInt(5);
        if(enemyChance > 1){
            attack(enemyName, playerName, enemyAttack, playerDefense, playerHealth);
        }
        else{
            System.out.println("\n" + enemyName + " missed!");
        }
        if(playerChance > 1){
            attack(playerName, enemyName, playerAttack, enemyDefense, enemyHealth);
        }
        else{
            System.out.println("\n" + playerName + " missed!");
        }
    }
    if(enemyHealth >= 0){
        System.out.println(enemyName + " has won...");
    }
    else if(playerHealth >= 0){
        System.out.println("You have won!");
    }
    return enemyHealth;
}

private static boolean bothAreAlive(int yourHealth, int enemyHealth){
    return yourHealth > 0 && enemyHealth > 0;
}

public static void action(int day){
    System.out.println("Day: " + day);
    System.out.println("\nWhat will you do today?\n(1)Fight boss\n(2)Fight random monster\n(3)Train\n(4)Show stats");
}

public static void showStats(String playerName, int health, int attack, int defense){
    System.out.println("Name: " + playerName);
    System.out.println("Health: " + health);
    System.out.println("Attack: " + attack);
    System.out.println("Defense: " + defense);
}

}

3 个答案:

答案 0 :(得分:0)

也许我读错了但似乎攻击方法并没有真正降低健康水平。

答案 1 :(得分:0)

  while(bothAreAlive(playerHealth, enemyHealth)){
        int playerChance = rng.nextInt(5);
        int enemyChance = rng.nextInt(5);
        if(enemyChance > 1){
            attack(enemyName, playerName, enemyAttack, playerDefense, playerHealth);
        }
        else{
            System.out.println("\n" + enemyName + " missed!");
        }
        if(playerChance > 1){
            attack(playerName, enemyName, playerAttack, enemyDefense, enemyHealth);
        }
        else{
            System.out.println("\n" + playerName + " missed!");
        }
    }

在Java中,您无法将int传递给方法,在方法中为其分配新值,并查看通过更改传递给方法的变量。
因此playerHealthenemyHealth永远不会在fight()方法中更新。

您在attack()方法中调用的fight()方法:

public static int attack(String attacker, String defender, int aAttack, int dDefense, int dHealth){

返回被攻击者的左侧生命值:玩家或敌人。
因此,您可以根据受到攻击的人将返回的值分配给playerHealth或enemyHealth。 例如:

  while(bothAreAlive(playerHealth, enemyHealth)){
        int playerChance = rng.nextInt(5);
        int enemyChance = rng.nextInt(5);
        if(enemyChance > 1){
            playerHealth = attack(enemyName, playerName, enemyAttack, playerDefense, playerHealth);
        }
        else{
            System.out.println("\n" + enemyName + " missed!");
        }
        if(playerChance > 1){
            enemyHealth = attack(playerName, enemyName, playerAttack, enemyDefense, enemyHealth);
        }
        else{
            System.out.println("\n" + playerName + " missed!");
        }
    }

答案 2 :(得分:0)

<强>问题

嗯,这里确切的问题是,正如你所提到的,war方法中的while循环无限期地运行。

导致此问题的原因是什么?

while循环中的条件始终为true,因为playerHealthenemyHealth值保持不变。

while(bothAreAlive(playerHealth, enemyHealth))

如果您认为如此,将playerHealthenemyHealth作为参数传递给攻击方法会改变其值,这是错误的。 Java是严格按值传递的,因为int是基本类型,它是方法的本地(复制的值传递给方法)。

<强>解决方案

请注意,您的攻击方法会返回新的健康dHealth,而不会返回您使用它的位置。 只需将返回的值恰当地分配给playerHealthenemyHealth,这样做就可以了。

playerHealth = attack(enemyName, playerName, enemyAttack, playerDefense, playerHealth);

enemyHealth = attack(playerName, enemyName, playerAttack, enemyDefense, enemyHealth);