【问题标题】:How to generate random numbers from 1 to 8 without repeating them? [duplicate]如何生成从 1 到 8 的随机数而不重复它们? [复制]
【发布时间】:2017-12-15 23:47:38
【问题描述】:

我使用 Java AWT 创建了以下随机播放拼图。如您所见,在程序开始时,所有数字都是按顺序排列的。我想生成随机数而不重复。这个怎么做?我应该使用Random 类吗?

import java.awt.*;
import java.awt.event.*;
class Puzzle extends Frame implements ActionListener
{
    Button b1,b2,b3,b4,b5,b6,b7,b8,b9;
    Puzzle()
    {
        setTitle("Shuffle");
        setSize(500,500);
        setLayout(new GridLayout(3,3));
        setVisible(true);
        Font f=new Font("Arial",Font.BOLD,100);

        b1=new Button("1");
                            b1.setFont(f);
                            b1.addActionListener(this);
        b2=new Button("2");
                            b2.setFont(f);
                            b2.addActionListener(this);
        b3=new Button("3");
                            b3.setFont(f);
                            b3.addActionListener(this);
        b4=new Button("4");
                            b4.setFont(f);
                            b4.addActionListener(this);
        b5=new Button("5");
                            b5.setFont(f);
                            b5.addActionListener(this);
        b6=new Button("6");
                            b6.setFont(f);
                            b6.addActionListener(this);
        b7=new Button("7");
                            b7.setFont(f);
                            b7.addActionListener(this);
        b8=new Button("8");
                            b8.setFont(f);
                            b8.addActionListener(this);
        b9=new Button(" ");
                            b9.setFont(f);
                            b9.addActionListener(this);

        add(b1); add(b2); add(b3);
        add(b4); add(b5); add(b6);
        add(b7); add(b8); add(b9);




    } 
    public void actionPerformed(ActionEvent ae)
    {
        if(ae.getSource() == b1)
        {
            if(b2.getLabel() == " ")
            {
                b2.setLabel(b1.getLabel());
                b1.setLabel(" ");
            }
        else if(b4.getLabel() == " ")
            {

              b4.setLabel(b1.getLabel());
                b1.setLabel(" ");
            }

        }
        if(ae.getSource() == b2)
        {
            if(b1.getLabel() == " ")
            {
                b1.setLabel(b2.getLabel());
                b2.setLabel(" ");
            }
        else if(b3.getLabel() == " ")
            {

              b3.setLabel(b2.getLabel());
                b2.setLabel(" ");
            }
            else if(b5.getLabel() == " ")
            {

              b5.setLabel(b2.getLabel());
                b2.setLabel(" ");
            }

        }
        if(ae.getSource() == b3)
        {
            if(b2.getLabel() == " ")
            {
                b2.setLabel(b3.getLabel());
                b3.setLabel(" ");
            }
        else if(b6.getLabel() == " ")
            {

              b6.setLabel(b3.getLabel());
                b3.setLabel(" ");
            }

        }
        if(ae.getSource() == b4)
        {
            if(b1.getLabel() == " ")
            {
                b1.setLabel(b4.getLabel());
                b4.setLabel(" ");
            }
        else if(b5.getLabel() == " ")
            {

              b5.setLabel(b4.getLabel());
                b4.setLabel(" ");
            }
            else if(b7.getLabel() == " ")
            {

              b7.setLabel(b4.getLabel());
                b4.setLabel(" ");
            }

        }
        if(ae.getSource() == b5)
        {
            if(b2.getLabel() == " ")
            {
                b2.setLabel(b5.getLabel());
                b5.setLabel(" ");
            }
        else if(b4.getLabel() == " ")
            {

              b4.setLabel(b5.getLabel());
                b5.setLabel(" ");
            }
            else if(b6.getLabel() == " ")
            {

              b6.setLabel(b5.getLabel());
                b5.setLabel(" ");
            }
            else if(b8.getLabel() == " ")
            {

              b8.setLabel(b5.getLabel());
                b5.setLabel(" ");
            }

        }
        if(ae.getSource() == b6)
        {
            if(b3.getLabel() == " ")
            {
                b3.setLabel(b6.getLabel());
                b6.setLabel(" ");
            }
        else if(b5.getLabel() == " ")
            {

              b5.setLabel(b6.getLabel());
                b6.setLabel(" ");
            }
        else if(b9.getLabel() == " ")
            {

              b9.setLabel(b6.getLabel());
                b6.setLabel(" ");
            }

        }
        if(ae.getSource() == b7)
        {
            if(b4.getLabel() == " ")
            {
                b4.setLabel(b7.getLabel());
                b7.setLabel(" ");
            }
        else if(b8.getLabel() == " ")
            {

              b8.setLabel(b7.getLabel());
                b7.setLabel(" ");
            }

        }
        if(ae.getSource() == b8)
        {
            if(b5.getLabel() == " ")
            {
                b5.setLabel(b8.getLabel());
                b8.setLabel(" ");
            }
        else if(b7.getLabel() == " ")
            {

              b7.setLabel(b8.getLabel());
                b8.setLabel(" ");
            }
            else if(b9.getLabel() == " ")
            {

              b9.setLabel(b8.getLabel());
                b8.setLabel(" ");
            }

        }
        if(ae.getSource() == b9)
        {
            if(b6.getLabel() == " ")
            {
                b6.setLabel(b9.getLabel());
                b9.setLabel(" ");
            }
        else if(b8.getLabel() == " ")
            {

              b8.setLabel(b9.getLabel());
                b9.setLabel(" ");
            }

        }

    }
    public static void main(String args[])
    {

      new Puzzle();
    }
}

【问题讨论】:

  • 也许从 1 - 8 创建一个数组,然后改组它? java.util.Collections.shuffle(List>)
  • 你尝试过洗牌吗?请显示该代码。是的,Math.random() 是一个很好的起点。
  • 在 b1=new Button("1") 的地方,我做了以下: [code] Random rand=new Random(); int a=rand.nextInt(); b1=新按钮(+a);但它重复了数字。 [\code]
  • 这不是你比较字符串的方式......

标签: java random awt shuffle


【解决方案1】:

一般答案:

编辑:我很抱歉,这与其他人发布的内容基本相同。他先搞定了。

ArrayList<Integer> draw = new ArrayList<Integer>();
for(int i = 1; i<=8; i++){
    draw.add(i); //Add every number once
}

//Every time you call it, you pick a random item from the list.
//Then you remove it from it to prevent being recalled. Therefore it won't be re-accesed.
Random r = new Random();
int placeholder = r.nextInt(draw.size());
int num = draw.get(placeholder);
draw.remove(placeholder);

对你来说,这是你应该做的:

...
ArrayList<Button> buttonlist = new ArrayList<Button>(); /// ADD THIS. Put this outside the method/function.
...
Puzzle(){
    setTitle("Shuffle");
    setSize(500,500);
    setLayout(new GridLayout(3,3));
    setVisible(true);
    Font f=new Font("Arial",Font.BOLD,100);

    b1=new Button("1");
                        b1.setFont(f);
                        b1.addActionListener(this);
                        buttonlist.add(b1); /// ADD THIS
    b2=new Button("2");
                        b2.setFont(f);
                        b2.addActionListener(this);
                        buttonlist.add(b2); /// ADD THIS
    b3=new Button("3");
                        b3.setFont(f);
                        b3.addActionListener(this);
                        buttonlist.add(b3); /// ADD THIS
    b4=new Button("4");
                        b4.setFont(f);
                        b4.addActionListener(this);
                        buttonlist.add(b4); /// ADD THIS
    b5=new Button("5");
                        b5.setFont(f);
                        b5.addActionListener(this);
                        buttonlist.add(b5); /// ADD THIS
    b6=new Button("6");
                        b6.setFont(f);
                        b6.addActionListener(this);
                        buttonlist.add(b6); /// ADD THIS
    b7=new Button("7");
                        b7.setFont(f);
                        b7.addActionListener(this);
                        buttonlist.add(b7); /// ADD THIS
    b8=new Button("8");
                        b8.setFont(f);
                        b8.addActionListener(this);
                        buttonlist.add(b8); /// ADD THIS
    b9=new Button(" ");
                        b9.setFont(f);
                        b9.addActionListener(this);
                        buttonlist.add(b9); /// ADD THIS
    Random r = new Random(); /// ADD THIS
    while(buttonlist.size() != 0){ /// ADD THIS
        int placeholder = r.nextInt(buttonlist.size()); /// ADD THIS
        Button button = buttonlist.get(placeholder); /// ADD THIS
        add(button); /// ADD THIS AFTER REPLACING all the original add(b1)...
        buttonlist.remove(placeholder); /// ADD THIS
    } /// ADD THIS
}

这与其他人建议给它一个单独的名字的反应有点不同,我相信这种方法会更好一些,所以 b1 没有标签 3 或其他东西。这样,随机顺序的答案将只是 b1,b2,b3 ......而不是随机顺序。这在实际改变边框上的按钮的同时保留了顺序,否则您将难以识别对象。

【讨论】:

  • 非常感谢。但是我应该如何将它放在按钮中?而且上述方法有时会重复相同的数字,对吗?
  • 它没有。因为它使用 draw.remove(placeholder); 将其从列表中删除;
  • @VishnuVardhanSIstla,这行得通吗?还是你有别的事情?
  • 它显示错误,找不到符号绘制。
  • @VishnuVardhanSIstla 对不起,我复制并粘贴了。那个“draw”应该是“buttonlist”。该问题将在几秒钟内得到解决。
【解决方案2】:

你可以这样做:

// global scope
List<Integer> container = new ArrayList<>();

//populate it
for(int i =1; i<=8; i++){
   container.add(i);
}
..........
..........
..........
public static int getRandom(){
    //to refill it if ya need to call it more than 8 times
    //if(container.size()==0){for(int i=1; i<=8; i++){container.add(i);} 
    Random generator = new Random();

    Integer randomNumber = container.get(generator.nextInt(container.size()));

    container.remove(randomNumber);

    return randomNumber;
}

此外,如果您想用 1 到 8 的唯一编号随机命名/标记每个按钮,只需执行以下操作:

b1=new Button(String.valueOf(getRandom()));

等等。

【讨论】:

  • 谢谢,但有时会重复相同的数字,对吗?
  • @VishnuVardhan 一点也不,返回后它永远不会重复数字。每次调用该方法时,它都会返回一个唯一编号。但是如果你想调用该方法超过 8 次,它会以防你重新填充它......不过我可以添加一些修改
  • 你说的我已经实现了。但是我应该如何将随机生成的数字添加到我的按钮?
  • @VishnuVardhan 你想用 1 到 8 的数字命名/标记每个按钮吗?
  • 是的。因为按钮显示随机生成的数字,用户必须按升序排列才能赢得游戏。
【解决方案3】:

由于您是从集合中随机选择一个号码,因此不要使用Random 号码生成器来选择号码,请使用Random 号码生成器从剩余的可用号码中进行选择。

 List<Integer> choices = getNewChoices();
 int choice = choices.remove(random.nextInt(choices.size());

选择最终会越来越小,但未被选中的元素仍在List中。

您可以将其包装成保护方法selectOne

public class Whatever {

     public void run() {
         ...
         List<Integer> choices = getNewChoices();
         ...
         int choice = selectOne(choices);
     }

     public List<Integer> getNewChoices() {
         return Arrays.toList(new Integer[] { 1, 2, 3, 4, 5, 6, 7, 8});
     }

     public int selectOne(List<Integer> choices) {
         if (choices.length() > 0) {
             return choices.remove(random.nextInt(choices.size());
         }
         throw new RuntimeException("out of choices");
     }
 }

【讨论】:

    猜你喜欢
    • 2017-05-28
    • 2015-07-12
    • 2017-10-22
    • 1970-01-01
    • 2013-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多