我的游戏代码中不需要的无限循环。如何突破呢?

时间:2015-01-26 01:11:11

标签: java loops

到目前为止,我自己已经找到了大部分代码。我试图制作一个基于文本的小游戏。我遇到麻烦的是,我似乎无法使随机数滚动正确循环。当它循环时,它只是循环最后滚动的东西。这是我的代码,整个事情。

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 个答案:

答案 0 :(得分:1)

这是因为你的Goblin的HP死后你不会重置它。您的代码非常庞大,但这就是为什么您的代码没有按预期工作的想法:

// 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:

答案 1 :(得分:0)

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 :(得分:0)

变量roll没有递增,因此您无休止地评估switch(randomNum)语句,其值为randomNum。这就是为什么即使在敌人被杀之后它仍然重复同样的情况。