简体   繁体   中英

Java how to get a random number every time a while loop begins?

This code is supposed to produce a new enemy every time one becomes dead. That includes the enemy producing a random bit of health but my program just uses the same health as the previous enemy therefore constantly looping to the "Enemy has been defeated," I was trying to get the enemy health and the specific enemy to be produced in the Enemy.java but I am struggling to get a grasp on proper OOP even though I've used a couple of tutorials.

  import java.util.Random;
  import java.util.Scanner;
  import java.util.concurrent.ThreadLocalRandom;


 public class Test {
 public static void main(String args[]) {

   Random rand = new Random();
   Scanner in = new Scanner(System.in);
   Scanner sc = new Scanner(System.in);


   boolean running = true;
   int hitPoints;
   int choice;
   String name;

  Vehicle Vehicle = new Vehicle();
  Player Player = new Player();
  Enemy Enemy = new Enemy();

  Player.name();
  Vehicle.number();
  Player.hitPoints();

  name = Player.name;
  int enemyHealth;

  GAME:
  while(running){

         String [] enemies = {"Skeleton", "Zombie", "Warrior", "Assassin", "Reaper", "Archer"};
         String enemy = enemies[rand.nextInt(enemies.length)];
         enemyHealth =  Player.enemyHealth;
         System.out.println("\n*# " + enemy + " appeared! #*");

         hitPoints = Player.hitPoints;

    while(enemyHealth > 0){

        enemyHealth =  Player.enemyHealth;
        if(enemyHealth < 1){
            break;
        }

        if(hitPoints < 1){
            System.out.println("\nYour car has been destroyed");
        break;
        }


      hitPoints = Player.hitPoints;

    System.out.println("\nYour car has " + hitPoints + " health ");
    System.out.println("Enemy car has " + enemyHealth + " health ");

    System.out.println("\n1. Bump enemy car");
    System.out.println("2. Heal your car");
    System.out.println("3. Avoid enemy car");
    int userChoice = in.nextInt();

    switch(userChoice){
    case 1:
        Player.attack();
        break;
    case 2:

    case 3:
        System.out.println("You barely avoided the enemy car");
        break;

}
}
if(hitPoints < 1){
    System.out.println("\nGame Over");
    break;
}
if(enemyHealth < 1){

hitPoints = Player.hitPoints;
  name = Player.name;
System.out.println(" \nEnd Fight Result: \n");
System.out.println(" # " + enemy + " was defeated! #");
System.out.println(" # You have " + hitPoints + " HP left. #");

System.out.println("\nWhat would you like to do " + name + " ?");
System.out.println("1. Continue Fighting");
System.out.println("2. Exit Dungeon");

choice = in.nextInt();

switch(choice){
    case 1:
        System.out.println("\nYou continue your adventure!");
        break;
    case 2:
        System.out.println("You exit the dungeon, succesful from your         adventures!");
        break;  
}
if(choice == 2){
    break;
}
}
  }
  name = Player.name;
  System.out.println("\n##########################");
    System.out.println("# Thanks for playing " + name + "! #");
    System.out.println("##########################");
}
}

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

 public class Player {

 Enemy Enemy = new Enemy();

Scanner in = new Scanner(System.in);
Scanner sc = new Scanner(System.in);
Random rand = new Random();
String name;
int hitPoints;
int enemyDamageDealt = Enemy.enemyDamageDealt;
int enemyHealth = Enemy.enemyHealth;
int damageDealt;

public void name(){

System.out.println("Enter your username");
this.name = sc.nextLine();
System.out.println("You set your username to: " + name);     

}

 public void hitPoints(){
 hitPoints = rand.nextInt(500) + 500;
}

public void attack(){

damageDealt = rand.nextInt(100);
enemyHealth -= damageDealt;
hitPoints -= enemyDamageDealt;
System.out.println("You damaged the enemy car for " + damageDealt);
System.out.println("In return you got damaged for " + enemyDamageDealt);
}
}


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

public class Enemy {

Scanner in = new Scanner(System.in);
Scanner sc = new Scanner(System.in);
Random rand = new Random();

 int enemyDamageDealt = rand.nextInt(100);

 public String [] enemies = {"Skeleton", "Zombie", "Warrior", "Assassin",   "Reaper", "Archer"};
 public String enemy = enemies[rand.nextInt(enemies.length)];



 int enemyHealth =  rand.nextInt(500) + 250;

 }

Ok your code is a little hard to follow. But I believe I found your issue.

You are setting

enemyHealth =  Player.enemyHealth;

Which if i'm reading correctly, enemyHealth in the Player class is actually set to

int enemyHealth = Enemy.enemyHealth;

Which... Enemy is an object that you only create once so it's setting the enemyHealth in the Enemy class and that's it.

int enemyHealth =  rand.nextInt(500) + 250;

There is no "easy" fix with the design of your code. Here's the list of problems I see with your code.

  1. Your Player class should not be keeping track of the enemy health. What if you want to add multiple enemies or change functionality of an enemy? It makes it very difficult to do if your Player class is keeping track of things not directly related to the Player.
  2. When you are declaring objects, stop doing Player Player = new Player() , and name your object properly like Player player = new Player() . Objects should start with a lowercase.
  3. The Attack method should not be in the Player class.
  4. You seem to be creating 2 instances of Enemy (One in the main class and one in the player class) and you are only using the one in the Player class.
  5. You should be creating a new enemy object and using its properties every time you create a new enemy.

With all that being said, I believe the easiest fix to your code is probably this, although I recommend making the above changes instead of this. Because this is not good code.

Under your while(running) , where you are setting your enemyHealth, replace it with these two lines.

enemyHealth =  rand.nextInt(500) + 250;
Player.enemyHealth = enemyHealth;

Isn't it easy? If you try just a small change and update the enemyHealth when there comes a new enemy:

        enemyHealth = rand.nextInt(500) + 250;
        Player.enemyHealth = enemyHealth;

Updated code

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

public class Main {
    public static void main(String args[]) {

        Random rand = new Random();
        Scanner in = new Scanner(System.in);
        Scanner sc = new Scanner(System.in);


        boolean running = true;
        int hitPoints;
        int choice;
        String name;

        // Vehicle Vehicle = new Vehicle();
        Player Player = new Player();
        Enemy Enemy = new Enemy();

        Player.name();
        //   Vehicle.number();
        Player.hitPoints();

        name = Player.name;
        int enemyHealth;

        GAME:
        while (running) {

            String[] enemies = {"Skeleton", "Zombie", "Warrior", "Assassin", "Reaper", "Archer"};
            String enemy = enemies[rand.nextInt(enemies.length)];
            enemyHealth = rand.nextInt(500) + 250;
            Player.enemyHealth = enemyHealth;
            System.out.println("\n*# " + enemy + " appeared! #*");

            hitPoints = Player.hitPoints;

            while (enemyHealth > 0) {

                enemyHealth = Player.enemyHealth;
                if (enemyHealth < 1) {
                    break;
                }

                if (hitPoints < 1) {
                    System.out.println("\nYour car has been destroyed");
                    break;
                }


                hitPoints = Player.hitPoints;

                System.out.println("\nYour car has " + hitPoints + " health ");
                System.out.println("Enemy car has " + enemyHealth + " health ");

                System.out.println("\n1. Bump enemy car");
                System.out.println("2. Heal your car");
                System.out.println("3. Avoid enemy car");
                int userChoice = in.nextInt();

                switch (userChoice) {
                    case 1:
                        Player.attack();
                        break;
                    case 2:

                    case 3:
                        System.out.println("You barely avoided the enemy car");
                        break;

                }
            }
            if (hitPoints < 1) {
                System.out.println("\nGame Over");
                break;
            }
            if (enemyHealth < 1) {

                hitPoints = Player.hitPoints;
                name = Player.name;
                System.out.println(" \nEnd Fight Result: \n");
                System.out.println(" # " + enemy + " was defeated! #");
                System.out.println(" # You have " + hitPoints + " HP left. #");

                System.out.println("\nWhat would you like to do " + name + " ?");
                System.out.println("1. Continue Fighting");
                System.out.println("2. Exit Dungeon");

                choice = in.nextInt();

                switch (choice) {
                    case 1:
                        System.out.println("\nYou continue your adventure!");
                        break;
                    case 2:
                        System.out.println("You exit the dungeon, succesful from your         adventures!");
                        break;
                }
                if (choice == 2) {
                    break;
                }
            }
        }
        name = Player.name;
        System.out.println("\n##########################");
        System.out.println("# Thanks for playing " + name + "! #");
        System.out.println("##########################");
    }
}


class Player {

    Enemy Enemy = new Enemy();

    Scanner in = new Scanner(System.in);
    Scanner sc = new Scanner(System.in);
    Random rand = new Random();
    String name;
    int hitPoints;
    int enemyDamageDealt = Enemy.enemyDamageDealt;
    int enemyHealth = Enemy.enemyHealth;
    int damageDealt;

    public void name() {

        System.out.println("Enter your username");
        this.name = sc.nextLine();
        System.out.println("You set your username to: " + name);

    }

    public void hitPoints() {
        hitPoints = rand.nextInt(500) + 5000;
    }

    public void attack() {

        damageDealt = rand.nextInt(100);
        enemyHealth -= damageDealt;
        hitPoints -= enemyDamageDealt;
        System.out.println("You damaged the enemy car for " + damageDealt);
        System.out.println("In return you got damaged for " + enemyDamageDealt);
    }
}


class Enemy {

    Scanner in = new Scanner(System.in);
    Scanner sc = new Scanner(System.in);
    Random rand = new Random();

    int enemyDamageDealt = rand.nextInt(100);

    public String[] enemies = {"Skeleton", "Zombie", "Warrior", "Assassin", "Reaper", "Archer"};
    public String enemy = enemies[rand.nextInt(enemies.length)];


    int enemyHealth = rand.nextInt(500) + 250;

}

Test (notice that it doesn't loop when there comes a new enemy).

Enter your username
foo
You set your username to: foo

*# Reaper appeared! #*

Your car has 5496 health 
Enemy car has 368 health 

1. Bump enemy car
2. Heal your car
3. Avoid enemy car
1
You damaged the enemy car for 36
In return you got damaged for 39

Your car has 5457 health 
Enemy car has 332 health 

1. Bump enemy car
2. Heal your car
3. Avoid enemy car
1
You damaged the enemy car for 61
In return you got damaged for 39

Your car has 5418 health 
Enemy car has 271 health 

1. Bump enemy car
2. Heal your car
3. Avoid enemy car
1
You damaged the enemy car for 42
In return you got damaged for 39

Your car has 5379 health 
Enemy car has 229 health 

1. Bump enemy car
2. Heal your car
3. Avoid enemy car
1
You damaged the enemy car for 2
In return you got damaged for 39

Your car has 5340 health 
Enemy car has 227 health 

1. Bump enemy car
2. Heal your car
3. Avoid enemy car
1
You damaged the enemy car for 69
In return you got damaged for 39

Your car has 5301 health 
Enemy car has 158 health 

1. Bump enemy car
2. Heal your car
3. Avoid enemy car
1
You damaged the enemy car for 45
In return you got damaged for 39

Your car has 5262 health 
Enemy car has 113 health 

1. Bump enemy car
2. Heal your car
3. Avoid enemy car
1
You damaged the enemy car for 23
In return you got damaged for 39

Your car has 5223 health 
Enemy car has 90 health 

1. Bump enemy car
2. Heal your car
3. Avoid enemy car
1
You damaged the enemy car for 81
In return you got damaged for 39

Your car has 5184 health 
Enemy car has 9 health 

1. Bump enemy car
2. Heal your car
3. Avoid enemy car
1
You damaged the enemy car for 16
In return you got damaged for 39

End Fight Result: 

 # Reaper was defeated! #
 # You have 5145 HP left. #

What would you like to do foo ?
1. Continue Fighting
2. Exit Dungeon
1

You continue your adventure!

*# Skeleton appeared! #*

Your car has 5145 health 
Enemy car has 284 health 

1. Bump enemy car
2. Heal your car
3. Avoid enemy car
1
You damaged the enemy car for 21
In return you got damaged for 39

Your car has 5106 health 
Enemy car has 263 health 

1. Bump enemy car
2. Heal your car
3. Avoid enemy car
1
You damaged the enemy car for 39
In return you got damaged for 39

Your car has 5067 health 
Enemy car has 224 health 

1. Bump enemy car
2. Heal your car
3. Avoid enemy car
1
You damaged the enemy car for 65
In return you got damaged for 39

Your car has 5028 health 
Enemy car has 159 health 

1. Bump enemy car
2. Heal your car
3. Avoid enemy car
1
You damaged the enemy car for 76
In return you got damaged for 39

Your car has 4989 health 
Enemy car has 83 health 

1. Bump enemy car
2. Heal your car
3. Avoid enemy car
1
You damaged the enemy car for 66
In return you got damaged for 39

Your car has 4950 health 
Enemy car has 17 health 

1. Bump enemy car
2. Heal your car
3. Avoid enemy car
1
You damaged the enemy car for 87
In return you got damaged for 39

End Fight Result: 

 # Skeleton was defeated! #
 # You have 4911 HP left. #

What would you like to do foo ?
1. Continue Fighting
2. Exit Dungeon
1

You continue your adventure!

*# Archer appeared! #*

Your car has 4911 health 
Enemy car has 701 health 

1. Bump enemy car
2. Heal your car
3. Avoid enemy car

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM