繁体   English   中英

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

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

这个代码应该在每次死亡时产生一个新的敌人。 这包括敌人产生随机的健康,但我的程序只使用与前一个敌人相同的健康,因此不断循环到“敌人被击败”,我试图让敌人的健康和特定的敌人产生于虽然我已经使用了几个教程,但是我很难掌握正确的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;

 }

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

你在设置

enemyHealth =  Player.enemyHealth;

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

int enemyHealth = Enemy.enemyHealth;

这...敌人是一个对象,因此它的设置,您只创建一次enemyHealth在敌对阶级,就是这样。

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

您的代码设计没有“简单”的修复。 这是我在代码中看到的问题列表。

  1. 你的玩家类不应该跟踪敌人的健康状况。 如果你想添加多个敌人或改变敌人的功能怎么办? 如果你的Player类跟踪与Player没有直接关系的东西,那么很难做到。
  2. 当您声明对象时,请停止Player Player = new Player() ,并将对象命名为Player player = new Player() 对象应以小写字母开头。
  3. Attack方法不应该在Player类中。
  4. 您似乎正在创建2个Enemy实例(一个在主类中,一个在播放器类中),并且您只使用Player类中的一个。
  5. 你应该创建一个新的敌人对象,并在每次创建一个新敌人时使用它的属性。

尽管如此 ,我相信对代码的最简单修复可能是这样, 尽管我建议进行上述更改而不是这样。 因为这不是好代码。

在你的while(running) ,你设置敌人健康的地方,用这两行替换它。

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

这不容易吗? 如果你遇到一个新的敌人,你只需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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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