从方法中调用Int?

时间:2013-03-27 01:56:10

标签: java random

我需要将一个使用4个随机不同数字(无重复)的int返回给main方法,以便我可以将其用于我的其余工作。我也不能使用字符串方法。我希望它打印随机数

这就是我所拥有的:

public static void main(String[] args) {
      int generateSecretNumber;
      System.out.print("Random number is: "+generateSecretNumber);

}

public static int generateSecretNumber(int w, int x, int y, int z, int secretNumber) {
    Random ran = new Random();

    w = ran.nextInt();
    x = ran.nextInt();
    y = ran.nextInt();
    z = ran.nextInt();


    while (w == x || w == y || w == z){
    w = ran.nextInt();
    }
    while (x == w || x == y || x==z){
    x = ran.nextInt();
    }
    while (y == w || y == x || y == z){
    y = ran.nextInt();
    }
    while (z == w|| z== x || z == y){
    z = ran.nextInt();
    }

    if (w != x && x != y && y!=z){

    secretNumber = w + x + y + z;
    }

    return secretNumber;
}

}

2 个答案:

答案 0 :(得分:1)

在您的代码中,您实际上从未调用generateSecretNumber()方法,而是声明变量。删除主页中的声明,并将打印行更改为System.out.print("Random number is: " + generateSecretNumber());

接下来,generateSecretNumber()方法不应该有任何参数,因为它确定它将完全由它自己完成,并且不需要任何外部数据。由于参数消失了,您还需要在其开头声明int w, x, y, z;

其次,你生成一个没有上限的随机整数。这根本不是您想要的,而是rand.nextInt()调用的上限应为10,从而产生rand.nextInt(10)。这将选择0到9之间的随机整数。

最后,您将返回数字的总和,而不是它们所做的实际四位数。相反,返回每个数字的总和乘以它们的位置。例如,第四个数字应为w * 1000

结果代码示例:

public class RandUniqueFourDigits {
    public static void main(String[] args) {
        System.out.println("Random number is: " + generateSecretNumber());
    }

    public static int generateSecretNumber() {
        Random rand = new Random();
        int w = 0, x = 0, y = 0, z = 0;

        // Generate each digit until they're all unique
        while(w == x || w == y || w == z || x == y || x == z || y == z) {
            w = rand.nextInt(10);
            x = rand.nextInt(10);
            y = rand.nextInt(10);
            z = rand.nextInt(10);
        }

        // Generate each digit until they're all unique
        w = rand.nextInt(10);
        do x = rand.nextInt(10); while(x == w)

        // Combine them into one integer and return
        return w * 1000 + x * 100 + y * 10 + z;
    }
}

对于更有效的循环(每个数字只在需要时重新生成,就像初始代码一样),用这个完全替换while循环:

w = rand.nextInt(10);
do x = rand.nextInt(10); while(x == w);
do y = rand.nextInt(10); while(y == w || y == x);
do z = rand.nextInt(10); while(z == w || z == x || z == y);

导致:

public class RandUniqueFourDigits {
    public static void main(String[] args) {
        System.out.println(generateSecretNumber());
    }

    public static int generateSecretNumber() {
        Random rand = new Random();
        int w = 0, x = 0, y = 0, z = 0;

        // Generate each digit until they're all unique
        w = rand.nextInt(10);
        do x = rand.nextInt(10); while(x == w);
        do y = rand.nextInt(10); while(y == w || y == x);
        do z = rand.nextInt(10); while(z == w || z == x || z == y);

        // Combine them into one integer and return
        return w * 1000 + x * 100 + y * 10 + z;
    }
}

答案 1 :(得分:0)

考虑到没有多少数字你可以在内存中计算所有数字并从中返回一个随机值。

public class RandomGenerator {
    private static final List<Integer> numbers;
    private static final Random random = new Random();

    static {
        for(int i = 1000; i < 10000; i++) {
            int x = i%10;
            int y = (i/10)%10;
            int z = (i/100)%100;
            int u = (i/1000);

            // Check for uniqueness
            if(unique) { numbers.add(i); }
        }
    }

    public static Integer getNumber() {
        return numbers.get(random.nextInt(numbers.size()));
    }
}