Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 基诺游戏中的随机数_Java_Oop - Fatal编程技术网

Java 基诺游戏中的随机数

Java 基诺游戏中的随机数,java,oop,Java,Oop,我开发了一个基诺游戏,我不知道如何从控制台中获取随机数,并将其放置到文本字段中。我试图用ArrayList或Array创建它,但它是错误的。 下面你可以找到我的代码。 谢谢你的帮助 import java.util.Random; public class Game { private final int RANDOMNUMBER = 80; private int prob; String k; String res; public void rN

我开发了一个基诺游戏,我不知道如何从控制台中获取随机数,并将其放置到文本字段中。我试图用ArrayList或Array创建它,但它是错误的。 下面你可以找到我的代码。 谢谢你的帮助

import java.util.Random;  
public class Game {

    private final int RANDOMNUMBER = 80;
    private int prob;
    String k;
    String res;
    public void rNumbers() {
        for (int x = 0; x <= 12; x++) {
            Random d = new Random();
            prob = d.nextInt(RANDOMNUMBER) + 0;
            System.out.println(prob);
        }
        k= Integer.toString(prob);
    }

    public String toString(){
        return ""+prob;
    }
}
import java.util.Random;
公开课游戏{
私人最终整数随机数=80;
私家侦探;
串k;
字符串res;
公共无效号码(){
对于(int x=0;x
  • 尝试使用Math类中的random()方法获取随机数

  • 使用previos建议,我认为您可以创建一个方法,用生成的随机数填充数组,然后可以将它们打印在控制台上或放置在gui中
  • 我希望我曾经帮助过你


    对不起,我是委内瑞拉。

    您可以使用
    ThreadLocalRandom.current().nextInt(origin,bound)获取特定范围内的随机数
    ,origin是包含的底数,bound是唯一的上限数。

    伙计们,我找到了一个解决方案。我在我的游戏类中创建了一个新变量
    StringBuilder sb=new StringBuilder();
    ,然后在我的方法中使用了
    sb.append(-”;
    sb.append(prob);
    成功了!

    到底出了什么问题?你能确定你想要显示一系列随机数吗?我想要一系列随机数问题是,当我在控制台上执行方法rNumbers()时,我有12个随机数。当我尝试“解析”时我只得到最后一个随机数,而不是其他11个。首先感谢你的回答。其次,我的问题不是随机数的范围,而是如何执行到JTextField区域。此外,我不知道这个解决方案是否需要将你写的代码行添加到JText的setter或getter字段,或者如果必须更改整个方法rNumbers().@MichalisTripolitsiotis这行代码每次运行时都返回一个整数。我建议声明一个字符串来保存文本字段的内容,并将每个随机数连接到该字符串的for循环中。然后您可以使用该字符串更新文本字段的内容。谢谢您的回答!我的问题不是如何获取random数字,但如何将它们输入到我的gui。我尝试了ArrayList和Array,但没有成功。当我运行程序并制作System.out.print时,所有数字都会显示出来,但当我调用text.setText方法时,只会显示最后一个数字。如果你有任何想法,我非常愿意讨论它。@MichalisTripolitsiotis现在我更了解了,问题是您只在prob属性中存储了最后生成的编号。
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    
    public class GuiKino extends JFrame implements ActionListener {
    
        private JPanel mainPanel;
        private JPanel contentPanel;
        private JPanel otherPanel;
    
        private JTextField text;
        private JButton rollButton;
    
        Game t = new Game();
    
        private JLabel[] a = new JLabel[80];
    
        GridLayout gridLayout = new GridLayout();
    
        public GuiKino() {
    
            this.setTitle("Kino");
    
            // Create panels
            mainPanel = new JPanel();
            contentPanel = new JPanel();
            otherPanel = new JPanel();
    
            this.contentPanel = (JPanel) getContentPane();
    
            contentPanel.add(otherPanel, BorderLayout.SOUTH);
            contentPanel.add(mainPanel, BorderLayout.CENTER);
    
            mainPanel.setLayout(new GridLayout(10, 10));
            otherPanel.setLayout(new GridLayout(1, 4));
    
            text = new JTextField("");
    
            rollButton = new JButton("Klirosi");
            rollButton.addActionListener(this);
    
            otherPanel.add(text);
            otherPanel.add(rollButton);
    
            for (int j = 0; j < a.length; j++) {
                a[j] = new JLabel("");
    
                a[j].setBorder(BorderFactory.createLineBorder(Color.BLACK));
            }
    
            //cell numbers
            for(int l=0; l<a.length; l++){
                a[l].setText(""+l);
            }
    
            for (int i = 0; i < a.length; i++) {
                mainPanel.add(a[i]);
                }
            }
    
        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == rollButton) {
                t.rNumbers();
                text.setText(t.toString());
                text.getText();
            }
        }
    }