简体   繁体   English

从不同的类生成2个数字时如何停止获取相同的数字

[英]how to stop getting the same number when generating 2 numbers from a different class

When I run this code i get 2 numbers (which is good) but the numbers generated are the same (which is bad) and I dont want the numbers to be the same. 当我运行此代码时,我得到2个数字(这是很好的),但是生成的数字是相同的(这是不好的),我不希望这些数字是相同的。 I've done this as an experiment for a rpg I was going to make so I thought it would be beter if each weapon had a different class. 我已经完成了这个工作,作为我要制作的rpg的实验,所以我认为如果每种武器使用不同的类,那就更好了。

The main class: 主班:

package battlesimMK2;
public class Main {

    public static void main(String Arg[]) {
        String WeponEquiped = "BasicAxe";
        System.out.print(BasicAxe.Str);
        System.out.print(BasicAxe.Str);
    }
}

The basic axe class: 基本的斧头类:

package battlesimMK2;
import java.util.Random;

public class BasicAxe {
    static Random rnd = new Random();
    static int Str = rnd.nextInt(4)+5;
}

This line: 这行:

static int Str = rnd.nextInt(4)+5;

declares a static variable and initializes it once . 声明一个静态变量并对其初始化一次 If you want the code to run each to you access Str , you should make it a method: 如果要让代码运行每个访问Str的代码,则应使其成为方法:

public static int getStrength() {
    return rnd.nextInt(4)+5;
}

Then call it with this code in Main.main : 然后在Main.main使用以下代码调用它:

System.out.print(BasicAxe.getStrength());
System.out.print(BasicAxe.getStrength());

An alternative which would probably be more object-oriented would be to make the strength an instance field, so that each axe created had a possibly-different (but persistent) strength: 一种可能更面向对象的替代方法是将强度设置为实例字段,以使创建的每个斧头都具有可能不同(但持久)的强度:

public class BasicAxe {
    private static final Random rnd = new Random();

    private final int strength;

    public BasicAxe() {
        strength = rnd.nextInt(4)+5;
    }

    public int getStrength() {
        return strength;
    }
}

Then in Main.main : 然后在Main.main

BasicAxe axe1 = new BasicAxe();
BasicAxe axe2 = new BasicAxe();
System.out.println(axe1.getStrength());
System.out.println(axe2.getStrength());
System.out.println(axe1.getStrength());

Here, the first and third lines of output will be the same - but the second will (probably) be different. 在这里,输出的第一行和第三行是相同的-但第二行(可能)是不同的。

This because this line 这是因为这条线

static int Str = rnd.nextInt(4)+5;

runs just one time in whole the lifecycle of your application. 在整个应用程序生命周期中仅运行一次。 It's static value, you should use static method instead. 它是静态值,您应该改用静态方法。

You're generating a single random number and printing it twice. 您正在生成一个随机数并打印两次。 Try something like this instead: 尝试这样的事情:

package battlesimMK2;
public class Main {

    public static void main(String Arg[]) {
        String WeponEquiped = "BasicAxe";
        System.out.print(BasicAxe.Str());
        System.out.print(BasicAxe.Str());
    }
}

package battlesimMK2;
import java.util.Random;

public class BasicAxe {
    static Random rnd = new Random();
    static int Str() { return rnd.nextInt(4)+5; }
}

Because you define the Str variable as static, only a single copy of that variable is shared between all your BasicAxe classes. 因为您将Str变量定义为静态变量,所以在所有BasicAxe类之间仅共享该变量的单个副本。

The way to get a different answer each time you ask for the int value is, to use the example posted by the previous poster, 每次要求输入int值时获得不同答案的方法是,使用上一个张贴者发布的示例,

String WeponEquiped = "BasicAxe";
System.out.print(BasicAxe.getStrength());
System.out.print(BasicAxe.getStrength());

But, if you want to create an actual instance of the class BasicAxe, which keeps it's value so that each time you ask for the strength you get the same value, you'll need something different. 但是,如果您想创建一个基本类BasicAxe的实际实例,以使其保持其值,以便每次您请求强度时都获得相同的值,那么您将需要不同的东西。

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

相关问题 如何从同一字段中具有不同类的JSON中读取数字? - How to read numbers from JSON with different class in the same field? 如何停止使用随机数获得相同的数字 - How to stop getting the same number using random 从外部类在For循环中生成随机数 - Generating Random Numbers in For Loop from External Class 生成随机数时获取非法参数异常 - Getting an illegalargumentexception when generating random number 生成随机数,每次给出相同的数字 - Generating random numbers giving the same number each time 如何从给定的四个数字中生成一个唯一的数字,以及如何从生成的唯一数字中获取这些给定的数字? - How to generate an unique number from a given four numbers and getting these given numbers back from generated unique number? 如何阻止用户再次选择相同的号码? - How to stop user from selecting the same number once more? 如何使用不同类别的同一个Webdriver - How to use the same Webdriver from different class 如何从不同的罐子加载相同的类 - how to load same class from different jars 在数组中搜索其他数字,当所有其他数字都相同时,可以使用分治法在O(logn)中完成 - Search the different number in array, when all the other numbers are same , can this be done in O(logn) using divide and conquer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM