-3

残りの作業で使用できるように、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;
}

}
4

2 に答える 2

1

コードでは、実際にgenerateSecretNumber()メソッドを呼び出すことはなく、代わりに変数を宣言しています。メインの宣言を削除し、印刷行をに変更しますSystem.out.print("Random number is: " + generateSecretNumber());

次に、generateSecretNumber()メソッドは引数を持たないようにする必要があります。これは、メソッドが完全にそれ自体で何を実行するかを決定し、外部データを必要としないためです。int w, x, y, z;引数がなくなったので、最初に宣言する必要もあります。

次に、上限のないランダムな整数を生成しています。rand.nextInt()これはあなたが望むものではありません。代わりに、呼び出しの上限は10である必要があり、結果としてrand.nextInt(10)。これにより、0から9までのランダムな整数が選択されます。

そして最後に、実際の4桁の数字ではなく、数字の合計を返します。代わりに、各桁の合計にその場所を掛けたものを返します。たとえば、4桁目は。である必要があります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;
    }
}
于 2013-03-27T02:10:32.790 に答える
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()));
    }
}
于 2013-03-27T02:11:10.970 に答える