2016-06-21 41 views
0

我想创建一个宾果游戏使用NetBeans IDE 6.9.1(我是编码noob)。现在我被困在尝试创建宾果卡。对于5x5卡的盒子,我使用jButtons。我不能将“B”宾果球随机分配到“B”列。我有一个“工作”的代码,用于随机化哪个“B”球进入其中的“B”jButton,但我的方法不适用于我用来输出随机抽取的宾果球的jLabel。这里是我的“随机B-球代码:”如何显示从java中的数组中随机选择的字符串没有重复

String[] Bball1 = {"B5", "B6", "B11"}; 
String Brandom1 = (Bball1[new Random().nextInt(Bball1.length)]); 
String[] Bball2 = {"B1", "B8", "B15"}; 
String Brandom2 = (Bball2[new Random().nextInt(Bball2.length)]); 
String[] Bball3 = {"B3", "B10", "B13"}; 
String Brandom3 = (Bball3[new Random().nextInt(Bball3.length)]); 
String[] Bball4 = {"B2", "B9", "B14"}; 
String Brandom4 = (Bball4[new Random().nextInt(Bball4.length)]); 
String[] Bball5 = {"B4", "B7", "B12"}; 
String Brandom5 = (Bball5[new Random().nextInt(Bball5.length)]); 

下面是当用户点击时,他们挑了宾果图案提交按钮,将产生的卡(不完全)的代码:

btnSubmit.setEnabled(false); 
    cboPattern.setEnabled(false); 
    btn1B.setText(Brandom1); 
    btn2B.setText(Brandom2); 
    btn3B.setText(Brandom3); 
    btn4B.setText(Brandom4); 
    btn5B.setText(Brandom5); 

是的,这是重复的,而不是过于随意,但我做了一些研究阵列,因为我还没有在我的计算机科学课堂上所学他们,并得到了这一点:

public static void main(String[] Bballs) { 
    String[] Bball; 
    Bball = new String[2]; 
    Bball[0] = "B1"; 
    Bball[1] = "B2"; 
    Bball[2] = "B3"; 

    int num = (int) (Math.random() * 2); 
    System.out.println(Bball[num]); 
} 

这仅仅是一个测试代码和y一样你可以看到我仍然可以获得B1超过一次,这不是我想要的宾果卡和随机挑选的宾果球(我还没有开始)。另外,每当我运行我的程序时,它都不会打印出最后一行。我需要在周三结束之前完成这项工作:/(这是一个很晚的国际滑联项目,不,我没有迟到)。谢谢你的时间。

+0

注意:Random.nextInt优于Math.random。见http://stackoverflow.com/questions/738629/math-random-versus-random-nextintint –

回答

1

我希望这不是太超前,你身在何处,但拉从N项目(球)包M元素,而无需重复最简单的方法,就是简单地将所有的元素添加到Listshuffle()它,然后使用subList()取第一个M值。

这里是这样做的一般方法:

private static List<String> draw(String ballPrefix, int noOfBalls, int noToDraw) { 
    List<String> balls = new ArrayList<>(); 
    for (int ballNo = 1; ballNo <= noOfBalls; ballNo++) 
     balls.add(ballPrefix + ballNo); 
    Collections.shuffle(balls); 
    return balls.subList(0, noToDraw); 
} 

测试

System.out.println(draw("B", 15, 5)); 

样本输出

[B9, B5, B3, B2, B15] 

UPDATE

好,在教学中位的利益,一个List有点类似数组,但更强大。它包含一系列值,如数组,并且这些值的索引从0开始,也像数组一样。

与数组,你在哪里得到使用arr.length长度,你叫list.size()列表。不要使用arr[2]检索值,请拨打list.get(2)

不是指定使用arr[2] = 7一个值,你可以调用list.set(2, 7)但是这是不寻常的。这是因为,不同于阵列正在使用new String[5]分配,其中分配5个值的一个固定大小的阵列,列表是使用new ArrayList()分配,并调用add()当根据需要将在大小增长,如上述方法一样。

是超级简短的介绍后,这里是你如何使用上面的方法:

List<String> balls = draw("B", 15, 5); 
btn1B.setText(balls.get(0)); 
btn2B.setText(balls.get(1)); 
btn3B.setText(balls.get(2)); 
btn4B.setText(balls.get(3)); 
btn5B.setText(balls.get(4)); 
+0

我得到一个错误为这行代码:'列表滚珠=新的ArrayList <>();'因为金刚石操作者不会在-source 1.6支承,我必须使用-source 7或更高。我尝试下载Java JDK 7,但它没有出现在“Source/Binary Format”下的组合框中。我的老师告诉我们不要更新NetBeans,因为新版本更难以使用。 – Michael

+0

@迈克尔所以只需添加类型:'新的ArrayList ()' – Andreas

+0

谢谢,但现在什么是我的错误:'私人静态列表平局(字符串ballPrefix,INT noOfBalls,诠释noToDraw){ 名单球=新的ArrayList (); noOfBalls = 15; noToDraw = 5; ballPrefix =“B”; 对于(int ballNo = 1; ballNo <= noOfBalls; ballNo ++){ balls.add(ballPrefix + ballNo); } Collections.shuffle(balls); return balls.subList(0,noToDraw); System.out.println(draw(ballPrefix,noOfBalls,noToDraw)); } – Michael

0

你必须知道的Math.random()给出了内0号和1

它从来没有给1

0 < Math.random() < 1 
0 < Math.random() * 2 < 2 
0 <= (int)(Math.random() * 2) <= 1 

注意地板的逻辑,它使你永远不会得到指数2

+0

对于没有重复,取while循环或列表类的样子 - 你在后面会学习。 – Tommy

0

不要尝试使用Math.random,你不会得到均匀的分布。使用List和Random.nextInt(int),你会得到一个相当简单的解决方案。

Random rand = new Random(); 
List<String> inRandomOrder = new ArrayList<>(); 
List<String> items = new ArrayList<>(); 
items.add("blah1"); 
items.add("blah2"); 
items.add("blah3"); 

while (!items.isEmpty()) { 
    String removed = items.remove(rand.nextInt(items.size())); 
    inRandomOrder.add(removed) 
} 

然后你有一个随机顺序你的项目列表。这不是真正的随机只是伪随机,如果你需要真正的随机,你将不得不使用SecureRandom。我相信这可能是一种更简单的方法,可以在Java 8流的功能上做到这一点,但是如果答案是老式的,我还没有很多时间来惹恼他们。

1

如果你想获得一个随机数,你可以使用Math.random()Random,这并不重要,你的问题。你的问题是你想要展示的。一种方法是从特殊的最大数量中随机获得,而最大值不会改变。我想这是你现在使用的。另一种方法是,当你从特殊的最大数量中得到一个随机数,然后删除随机数,确保每个数字(从最小到最大)都将被使用。 Java有一个Collections.java的工具,它具有shuffle的功能,它可以使整个列表随机。以下是您的示例测试代码。如果您希望每个数字(从最小到最大)以相同的速度出现,您可以使用averageRandom_1averageRandom_2

import java.util.*; 
public class Main { 

    public static void main(String[] args) { 
     System.out.println("Hello World!"); 
     String[] src = new String[]{"B1", "B2", "B3", "B4", "B5"}; 
     List<String> srcList = Arrays.asList(src); 

    /*  for(int i = 0; i < srcList.size(); i++){ 
      System.out.print(" random = " + getRandom_2(0, srcList.size())); 
     }*/ 

     averageRandom_2(srcList); 
    } 

/** 
* @param min 
* @param max 
* @return [min, max), note it can't return max 
*/ 
public static int getRandom_1(int min, int max){ 
    return (int)(min + Math.random()*(max-min)); 
} 

/** 
* @param min 
* @param max 
* @return [min, max), note it can't return max 
*/ 
public static int getRandom_2(int min, int max){ 
    Random random = new Random(); 
    return random.nextInt(max-min) + min; 
} 

    public static void averageRandom_1(List<String> data){ 
     List<String> dataCopy = new ArrayList<>(); 
     dataCopy.addAll(data); 
     Collections.shuffle(dataCopy); 
     for(String item : dataCopy){ 
      System.out.println("--" + item + "--"); 
     } 
    } 

    public static void averageRandom_2(List<String> data){ 
     List<String> dataCopy = new ArrayList<String>(data); 
     while(dataCopy.size() > 0) { 
      int random = getRandom_2(0, dataCopy.size()); 
      System.out.println("random = " + random + "--" + dataCopy.get(random) + "--"); 
      dataCopy.remove(random); 
     } 
    } 
} 

请记住random.nextInt(value)返回0到(值-1)。希望能帮助你。

相关问题