2016-08-04 73 views
-3

我想在指定的數字列表中生成兩個數字。我寫了這段代碼,但它不起作用。任何人都可以提出一個更好的解從指定的數字列表中生成兩個數字

public classl { 

    public static void main(String[] args) { 

     int[] lottery = new int[2]; 
     int randomNum; 

     for (int i = 0; i < 3; i++) { 
      randomNum = (int) (Math.random() * (3,9,25,41,43,50,68)); // Random  number created here. 
      for (int x = 0; x < i; x++) { 
       if (lottery[x] == randomNum) // Here, code checks if same random number generated before. 
       { 
        randomNum = (int) (Math.random() * (3,9,25,41,43,50,68));// If random number is same, another number generated. 
        x = -1; // restart the loop 
       } 

      } 
      lottery[i] = randomNum; 
     } 

     for (int i = 0; i < lottery.length; i++) 
      System.out.print(lottery[i] + " "); 
     } 

    } 
} 
+2

爲什麼在'Math.random()*'後面有逗號分隔的數字列表? –

+0

爲了回答您的問題並解決代碼中的所有錯誤,我們基本上必須向您介紹Java編程語言。堆棧溢出不是地方的任務。 –

回答

0

您可以在這樣一個數組存儲這些數字:

int[] arr = {12, 13, 4, 7, 9}; 

然後得到一個隨機數ARR陣列:

int rndNumber = new Random().nextInt(arr.length); 

這是一些想法供你解決你的問題。

+1

我認爲OP會比這更需要更多的幫助。 –

0

我想你問的是如何從集合中選擇給定數量的隨機元素。

如果這是正確的,那麼你最好從一個集合而不是一個數組開始,因爲集合中有很好的方法可以幫助你。這裏有一些可能的方法:

List<Integer> theNumbers = Arrays.asList(3, 9, 25, 41, 43, 50, 68); 
Random random = new Random(); 

// using uniqueness of sets 
Set<Integer> results = new HashSet<>(); 
while (results.size() < 2) 
    results.add(theNumbers.get(random.nextInt(theNumbers.size())); 

// using shuffle 
Collections.shuffle(theNumbers); 
List<Integer> results = theNumbers.sublist(0, 2); 

// using streams (Java 8) 
int[] results = random.ints(0, theNumbers.size()) 
    .map(theNumbers::get).distinct().limit(2).toArray(); 

還有很多其他的方法,但希望這會讓你開始。

0

sprinter的答案是優雅的做法。如果你很難理解它作爲一個初學者,下面的例子將幫助你理解我們是如何做到這一點

import java.util.HashSet; 
import java.util.Random; 
import java.util.Set; 

public class LotteryGenerator { 

    public static void main(String[] args) { 

     //declare specified list of numbers 
     int[] lotteryBank = {3, 9, 25, 41, 43, 50, 68}; 
     //get lottery numbers 
     int[] lottery = getLottery(lotteryBank); 

     System.out.println("The two lottery numbers are : [" + lottery[0] + ", " + lottery[1] + "]"); 

    } 

    public static int[] getLottery(int[] lotteryBank) { 
     //find the total number of elements, so that random number generated is one among these 
     int indexRange = lotteryBank.length; 
     //create an array to hold two random numbers 
     int[] lottery = new int[2]; 
     //create an instance of Random 
     Random r = new Random(); 
     //create a set to hold two unique random numbers 
     Set<Integer> randomSet = new HashSet<Integer>(); 

     //iterate till two unique random numbers are found 
     while(randomSet.size() != 2) { 
      //get a random index 
      int randomIndex = r.nextInt(indexRange); 
      //add the element at random index to the set 
      randomSet.add(lotteryBank[randomIndex]); 
     } 

     //convert random set to random array 
     int index = 0; 
     for(int x : randomSet) { 
      lottery[index++] = x; 
     } 
     return lottery; 
    } 
} 

注:我不鼓勵使用INT []存儲的彩票號碼。實際上,不要使用原始數組,而是使用java提供的集合,它提供了很多實用程序方法,就像sprinter指出的那樣。這個解決方案的目的只是爲了告訴你如何做到這一點,你的方法傾向於