Java如何在每次while循环开始时获取一个随机数?

时间:2016-12-30 19:14:33

标签: java

这个代码应该在每次死亡时产生一个新的敌人。这包括敌人产生随机的健康状况,但我的程序只使用与前一个敌人相同的健康状况,因此不断循环到"敌人被击败,"我试图在Enemy.java中获得敌人的健康和特定的敌人,但即使我已经使用过几个教程,我仍在努力掌握正确的OOP。

  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;

 }

2 个答案:

答案 0 :(得分:0)

好的,你的代码有点难以理解。但我相信我找到了你的问题。

您正在设置

enemyHealth =  Player.enemyHealth;

如果我正确阅读,那么Player类中的enemyHealth实际上设置为

int enemyHealth = Enemy.enemyHealth;

其中...... Enemy是一个只创建一次的对象,所以它在敌人类中设置enemyHealth就是这样。

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

代码设计没有“简单”的解决方法。以下是我在您的代码中看到的问题列表。

  1. 你的玩家类不应该跟踪敌人的健康状况。如果你想添加多个敌人或改变敌人的功能怎么办?如果你的Player类跟踪与玩家没有直接关系的东西,那么很难做到这一点。
  2. 当您声明对象时,请停止Player Player = new Player(),并像Player player = new Player()一样正确命名对象。对象应以小写字母开头。
  3. Attack方法不应该在Player类中。
  4. 您似乎正在创建2个Enemy实例(一个在主类中,一个在播放器类中),并且您只使用了Player类中的一个。
  5. 每次创建新敌人时,您应该创建一个新的敌人对象并使用其属性。
  6. 尽管如此,我相信对代码的最简单修复可能就是这样,虽然我建议进行上述修改而不是这个。因为这不是好的代码。

    在你设置敌人健康的while(running)下,用这两行代替。

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

答案 1 :(得分:0)

不容易吗?如果您只是进行了一些小改动,并在有新敌人时更新enemyHealth

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

更新了代码

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;

}

测试(注意当有新敌人时它不会循环)。

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