开发者

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. 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.

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:

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

Then call it with this code in 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:

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.


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; }
}


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.


Because you define the Str variable as static, only a single copy of that variable is shared between all your BasicAxe classes.

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,

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜