2012-11-21 47 views
1

我正在写一个程序,模拟1到45之间的六个数字的乐透抽签,样例输出是3 7 12 27 43 28.但是我想要做的是计算相邻数字出现的次数,例如1 4 5 29 26 41是一个肯定的答案,因为5在4之后。Java lotto模拟

这样做的最佳方式是什么?

我已经试过的例子,如:

int adjacent=0; 

    for(int i =0; i<6; i++) 
    { 
     int t = test[i]+1; 
     test[i]=(int)(45*Math.random())+1; 

     if(test[i]==t) 
      adjacent++; 

     System.out.print(test[i]+" "); 
    } 

这是行不通的。

我在做什么错?

+1

输出是'1 4 29 5 26 41',你仍然会计数4和5吗? –

+0

不,他们必须在旁边 – user1816464

+0

结果的顺序是否重要?你的乐透可以重复吗? –

回答

0

我认为你有操作问题

int adjacent=0; 

for(int i =0; i<6; i++) 
{ 
    //test[i] hasn't been set yet 
    int t = test[i]+1; 
    test[i]=(int)(45*Math.random())+1; 

    //this comparison doesn't make a whole lot of sense 
    if(test[i]==t) 
     adjacent++; 

    System.out.print(test[i]+" "); 
} 

改变它周围的像这样的顺序:

int adjacent=0; 

for(int i =0; i<6; i++) 
{ 
    test[i]=(int)(45*Math.random())+1; 
    int t = -1; 
    //Make sure this comparison only happens after the second iteration 
    //to avoid index out of bounds 
    if (i != 0) 
    { 
     //Set t to the last number + 1 instead of trying to predict the future 
     t = test[i-1] + 1; 
    } 
    //Now this comparison makes a little more sense 
    //The first iteration will compare to -1 which will always be false 
    if(test[i]==t) 
     adjacent++; 

    System.out.print(test[i]+" "); 
} 

这可以进一步简化为这样:

int adjacent=0; 

for(int i =0; i<6; i++) 
{ 
    test[i]=(int)(45*Math.random())+1; 

    if(i != 0 && test[i]==(test[i-1]+1)) 
     adjacent++; 

    System.out.print(test[i]+" "); 
} 
+0

所以这会给我一个积极的结果,如果是8,然后是7,或者我误解了代码。 – user1816464

+0

@ user1816464应该是相反的,我在缩短版本中加了一个加号,现在已经修复了。 –

+0

谢谢你现在的作品!谢谢你的所有解释和帮助! – user1816464

0

我认为你应该洗牌,并采取任何五。 Collections#Shuffle会帮助你,它使用默认的随机源来排列指定的列表。所有排列发生的可能性几乎相等。

List<Integer> list = ArrayList<Integer>(); 
list.add(1); 
list.add(2); 
Collections.shuffle(list); 
Random rnd = new Random(); 
Integer[] result = new Integer[5]; 
result[0] = list.get(rnd.getNextInt(45)); 
result[1] = list.get(rnd.getNextInt(45)); 
result[2] = list.get(rnd.getNextInt(45)); 
result[3] = list.get(rnd.getNextInt(45)); 
result[4] = list.get(rnd.getNextInt(45)); 

它总是给你随机值,那么你应该对它进行排序,按顺序排列,比如升序排列。

Arrays.sort(result); 

现在你可以写一个循环来找出相邻的数字。

int adjacent = 0; 
for(int i=1; i<result.length;i++){ 
    int prev = result[i-1]; 
    int now = result[i]; 
    if(prev+1 == now) 
    adjacent++; 
} 
+0

我不确定这是否回答他的问题。但是这比使用随机生成6个乐透号码更好。 –

+0

问题是关于检测相邻值,而不是如何首先生成随机数。 –

+0

是的,我只是想展示正确的方式来做到这一点。无论如何'现在的任何理由'? –

0

生成6个数字并将它们放入数组后。使用Arrays.sort()。然后您可以比较相邻的数组条目。

你也应该避免使用随机生成6个数字,因为它可以生成重复。这可能会或可能不会准确地模拟您的乐透抽奖。 Quoi的回答对此有很好的建议。

+0

downvote的原因? –

0

你需要分开独特的生成(因此下面的HashSet来保证身份)随机选择,对它们进行排序,然后确定邻接关系:

import java.util.HashSet; 
import java.util.Arrays; 

public class Lotto 
{ 
    public Lotto() 
    { 

    } 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) 
    { 
     Lotto lotto = new Lotto(); 
     lotto.randomizeSelections(5); 
    } 

    private void randomizeSelections(int numOfArrays) 
    { 
     for(int i = 0; i < numOfArrays; i++) 
     { 
      int[] selArry = new int[6]; 
      //to insure that each random selection is unique 
      HashSet<Integer> idntySet = new HashSet<Integer>(); 

      for(int j = 0; j < 6;) 
      { 
       int rndm = (int)(45 * Math.random()) + 1; 

       //add selection to the array only if it has not been randomized before 
       if(!idntySet.contains(rndm)) 
       { 
        selArry[j] = rndm; 

        j++; 
       } 

      } 

      //sort the array for determing adjacency 
      Arrays.sort(selArry); 

      for(int j = 0; j < 6; j++) 
      { 
       int sel = selArry[j]; 
       boolean isAdjcnt = (j > 0 && (sel == selArry[j - 1] + 1)) ? true : false; 

       System.out.println(i + "." + j + ".random = " + sel); 

       if(isAdjcnt) System.out.println("\tAdjacent"); 

      } 
     } 
    } 
}