有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

我的游戏代码中有一个不想要的无限循环。如何摆脱它?

到目前为止,大部分代码我都是自己编出来的。我想做一个基于文本的小游戏。我遇到的问题是,我似乎无法使随机数滚动正确循环。当它循环时,它只是循环最后一个滚动的东西。这是我的代码,全部内容

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

//Setting the variables for monsters
class monster {
    int health;
    String name;
    int damage;
}

//Setting the Variables that the player has
class Player {
    String name;
    int health;
    int damage;
}


public class Main {

public static void main(String[] args) {

    //Creating a scanner to receive user input
    Scanner scanner1 = new Scanner(System.in);

    //Creating Monsters
    monster Goblin = new monster();
    Goblin.name = "Goblin";
    Goblin.health = 5;
    Goblin.damage = 1;

    monster Orc = new monster();
    Orc.name = "Orc";
    Orc.health = 8;
    Orc.damage = 3;

    //Creating player stats
    Player Player = new Player();
    Player.name = "";
    Player.health = 15;
    Player.damage = 3;

    //////////////////////////////////////

    //Beginning of the game
    System.out.println("Hello adventurer. What is your name?");

    //Receive user input for their name
    String Username = scanner1.nextLine();
    Player.name = Username;

    //Message to restate player's name
    System.out.println("Ah, so your name is " + Username + ". Pleasure to meet you.");

    //Continuing description.
    System.out.println("Sorry to say, but there are more pressing matters at hand.");
    System.out.println("The nearby town of Notsosafe has been overrun by monsters!");
    System.out.println("Please go and help clear out the monsters there.");
    System.out.println("What is your weapon of choice? The Sword and shield? Greatsword? or Staff?");

    //Check for user input
    String weapon = scanner1.nextLine();

    //Check for which stats to apply
    while(true)
        if(weapon.equals("Sword and shield")){
            Player.damage = 3 + 2;
            Player.health = 15 + 3;
            break;
        }
        else if(weapon.equals("Greatsword")){
            Player.damage = 3 + 5;
            break;
        }
        else if(weapon.equals("Staff")){
            Player.damage = 3 + 1;
            Player.health = 15 + 5;
            break;
        }
        else{
            System.out.println("We don't seem to have that, Please pick what we have.");
            weapon = scanner1.nextLine();
        }
    //Displays the Player's stats
    System.out.println("Your stats are now:");
    System.out.println("Attack: " + Player.damage);
    System.out.println("Health: " + Player.health);
    System.out.println(" ");
    System.out.println("Now that you have selected your weapon, Go and save the town of Notsosafe!");

    //Rolls random number to decide encounter
    Random dice = new Random();
    int randomNum = dice.nextInt((6-1) + 1) + 1;
    System.out.println(randomNum);

    int roll = 0;
    //Actions corresponding to random roll and which actions to take
    while(true)
        if(roll < 10){
    switch(randomNum){

    case 1:
        System.out.println("You've traveled some miles without any encounter.");

        break;

    case 2:
        System.out.println("You've traveled some miles without any encounter.");

        break;

    case 3:
        System.out.println("You've traveled some miles without any encounter.");

        break;

    case 4:
        System.out.println("You've run into a goblin! Attack or Defend?");
        String action = scanner1.nextLine();
        while(Goblin.health > 0){
            if(action.equals("Attack")){
                Goblin.health = Goblin.health - Player.damage;
                System.out.println("You've done " + Player.damage + " damage to Goblin. Goblin has "+ Goblin.health + " health remaining.");
                while(true){
                if(Goblin.health <= 0){
                    System.out.println("You have slain the Goblin! Continue to move forward.");
                    break;
                }
                    else{

                        Player.health = Player.health - Goblin.damage;
                        System.out.println("Goblin attacked you for " + Goblin.damage + " damage.");
                        System.out.println("You have " + Player.health + " health remaining.");
                        System.out.println("Will you Attack or Defend?");
                        action = scanner1.nextLine();
                        if(action.equals("Attack")){
                            Goblin.health = Goblin.health - Player.damage;
                            System.out.println("You've done " + Player.damage + " damage to Goblin. Goblin has "+ Goblin.health + " health remaining.");
                            continue;
                            }

                        }
                    }
                }
            else if(action.equals("Defend")){
                System.out.println("You defended against the attack.");
                System.out.println("Goblin attacked you for " + Goblin.damage/2 + " damage.");
                System.out.println("You have " + Player.health + " health remaining.");
                Goblin.health = Goblin.health - Player.damage/2;
                System.out.println("You counter-attack for " + Player.damage/2 + " damage. Goblin has " + Goblin.health + " health remaining");
                Player.health = Player.health - Goblin.damage/2;
                    if(Goblin.health <= 0){
                        System.out.println("You have slain the Goblin! Continue to move forward.");
                        break;
                    }

                            else{
                                System.out.println("Will you Attack or Defend?");
                                action = scanner1.nextLine();
                                if(action.equals("Defend")){
                                    continue;
                                }                               
                        }
                }   
            }
        break;

    case 5:
        System.out.println("You've run into a goblin! Attack or Defend?");
        String action1 = scanner1.nextLine();
        while(Goblin.health > 0){
            if(action1.equals("Attack")){
                Goblin.health = Goblin.health - Player.damage;
                System.out.println("You've done " + Player.damage + " damage to Goblin. Goblin has "+ Goblin.health + " health remaining.");
                while(true){
                if(Goblin.health <= 0){
                    System.out.println("You have slain the Goblin! Continue to move forward.");
                    break;
                }
                    else{

                        Player.health = Player.health - Goblin.damage;
                        System.out.println("Goblin attacked you for " + Goblin.damage + " damage.");
                        System.out.println("You have " + Player.health + " health remaining.");
                        System.out.println("Will you Attack or Defend?");
                        action1 = scanner1.nextLine();
                        if(action1.equals("Attack")){
                            Goblin.health = Goblin.health - Player.damage;
                            System.out.println("You've done " + Player.damage + " damage to Goblin. Goblin has "+ Goblin.health + " health remaining.");
                            continue;
                            }

                        }
                    }
                }
            else if(action1.equals("Defend")){
                System.out.println("You defended against the attack.");
                System.out.println("Goblin attacked you for " + Goblin.damage/2 + " damage.");
                System.out.println("You have " + Player.health + " health remaining.");
                Goblin.health = Goblin.health - Player.damage/2;
                System.out.println("You counter-attack for " + Player.damage/2 + " damage. Goblin has " + Goblin.health + " health remaining");
                Player.health = Player.health - Goblin.damage/2;
                    if(Goblin.health <= 0){
                        System.out.println("You have slain the Goblin! Continue to move forward.");
                        break;
                    }

                            else{
                                System.out.println("Will you Attack or Defend?");
                                action1 = scanner1.nextLine();
                                if(action1.equals("Defend")){
                                    continue;
                                }                               
                        }
                }   
            }
        break;
    case 6:
        System.out.println("You've run into an Orc! Attack or Defend?");
        String action2 = scanner1.nextLine();
            while(Orc.health > 0){
                if(action2.equals("Attack")){
                    Orc.health = Orc.health - Player.damage;
                    System.out.println("You've done " + Player.damage + " damage to Orc. Orc has "+ Orc.health + " health remaining.");
                    while(true){
                    if(Orc.health <= 0){
                        System.out.println("You have slain the Orc! Continue to move forward.");
                        break;
                    }
                        else{

                            Player.health = Player.health - Orc.damage;
                            System.out.println("Orc attacked you for " + Orc.damage + " damage.");
                            System.out.println("You have " + Player.health + " health remaining.");
                            System.out.println("Will you Attack or Defend?");
                            action2 = scanner1.nextLine();
                            if(action2.equals("Attack")){
                                Orc.health = Orc.health - Player.damage;
                                System.out.println("You've done " + Player.damage + " damage to Orc. Orc has "+ Orc.health + " health remaining.");
                                continue;
                                }

                            }
                        }
                    }
                else if(action2.equals("Defend")){
                    System.out.println("You defended against the attack.");
                    System.out.println("Orc attacked you for " + Orc.damage/2 + " damage.");
                    System.out.println("You have " + Player.health + " health remaining.");
                    Orc.health = Orc.health - Player.damage/2;
                    System.out.println("You counter-attack for " + Player.damage/2 + " damage. Orc has " + Orc.health + " health remaining");
                    Player.health = Player.health - Orc.damage/2;
                        if(Orc.health <= 0){
                            System.out.println("You have slain the Goblin! Continue to move forward.");
                            break;
                        }

                                else{
                                    System.out.println("Will you Attack or Defend?");
                                    action2 = scanner1.nextLine();
                                    if(action2.equals("Defend")){
                                        continue;
                                    }                               
                            }
                    }   
                }
            roll = roll + 1;
        }
        }
            else{
                break;
            }

    }
}

下面是输出的情况

Hello adventurer. What is your name?
Testrun
Ah, so your name is Testrun. Pleasure to meet you.
Sorry to say, but there are more pressing matters at hand.
The nearby town of Notsosafe has been overrun by monsters!
Please go and help clear out the monsters there.
What is your weapon of choice? The Sword and shield? Greatsword? or Staff?
Staff
Your stats are now:
Attack: 4
Health: 20

Now that you have selected your weapon, Go and save the town of Notsosafe!
4
You've run into a goblin! Attack or Defend?
Attack
You've done 4 damage to Goblin. Goblin has 1 health remaining.
Goblin attacked you for 1 damage.
You have 19 health remaining.
Will you Attack or Defend?
Attack
You've done 4 damage to Goblin. Goblin has -3 health remaining.
You have slain the Goblin! Continue to move forward.
You've run into a goblin! Attack or Defend?
Attack
You've run into a goblin! Attack or Defend?
Attack
You've run into a goblin! Attack or Defend?
Attack
You've run into a goblin! Attack or Defend?

在你杀死了地精之后,它只是循环问题,而不是实际返回并运行伤害代码等等。我做错了什么


共 (3) 个答案

  1. # 1 楼答案

    mauzel是正确的。你不重置健康,因此它卡在循环中

    while(Goblin.health > 0)
    

    我想补充一下答案。不要总是在下面使用

    while(true)
    

    如果不正确地中断,它会导致无限循环。而是使用一面旗帜

        boolean something = true
        while(something)
        {
          //do something
        }
    

    因此,您可以在需要时轻松地将标志设置为false

    此外,当你进入循环时,你得到了

    while(true)
            if(roll < 10){
    }
    

    我建议删除if条件并将其更改为

    while (roll != 10)
    {
    }
    

    尝试将所有事件分解成方法,以便调试和发现问题,我希望您使用的是调试器(调试时很容易发现)

  2. # 2 楼答案

    变量roll不是递增的,因此您将使用不变的值randomNumswitch(randomNum)语句进行无休止的求值。这就是为什么即使在敌人被杀后,它仍在重复同样的情况

  3. # 3 楼答案

    这是因为你没有在妖精死亡后重置它的生命。您的代码非常庞大,但以下是您的代码无法按预期工作的原因:

    // You only create a single Goblin instance in your scope
    monster Goblin = new monster();
    Goblin.name = "Goblin";
    Goblin.health = 5;
    Goblin.damage = 1;
    
    ...
    
    while(true)
        if(roll < 10) {
            switch(randomNum) {
            ...
            case 4:
                System.out.println("You've run into a goblin! Attack or Defend?");
                String action = scanner1.nextLine();
    
                while(Goblin.health > 0) {
                    if(action.equals("Attack")) {    
                        Goblin.health = Goblin.health - Player.damage;
                        System.out.println("You've done " + Player.damage + " damage to Goblin. Goblin has "+ Goblin.health + " health remaining.");
                        while(true){
                            if(Goblin.health <= 0){
                                 System.out.println("You have slain the Goblin! Continue to move forward.");
                                 break;
                            }
                        }
    
                    } else if(action.equals("Defend")) {
                        // Redacted
                    }
                }
    
                // You need to reset the Goblin's health here.
                break;
            case 5:
                ...
            }
        }
    

    你永远不会重置地精的健康

    简单的解决方法是将case 4的结尾改为:

    case 4:
        // All your other handling here
    
        Goblin.health = 5;
        break;
    case 5: