java - 如何选择一个随机数并保存它?

标签 java

我正在尝试制作一对纸牌游戏,当小程序启动时,纸牌面朝下,带有“****”,当按下 JButton 时,它会根据随机拾取的数字放置一个数字,我只是想要为 2 张卡提供相同的随机数,并且每次按下 JButton 时数字不必改变,但我不知 Prop 体该怎么做

import javax.swing.*;
import java.util.Stack;
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
import java.util.*;
public class Memorando extends Applet implements ActionListener {
    JButton cartas[];
    JButton juegoNuevo;
    Label marcador;

    public void init(){
        this.setLayout(new BorderLayout());
        this.setBackground(Color.CYAN);
        Font appletFont=new Font("Monospased", Font.BOLD, 20);
        this.setFont(appletFont);
        juegoNuevo=new JButton("Juego nuevo");
        juegoNuevo.addActionListener(this);
        Panel topPanel=new Panel();
        topPanel.add(juegoNuevo);
        this.add(topPanel,"North");
        cartas=new JButton[8];

        Panel panelCentral=new Panel();
        panelCentral.setLayout(new GridLayout(2,4));
        this.add(panelCentral,"Center");
        marcador=new Label("No has ganado aun :(");
        this.add(marcador,"South");
        for(int i=0;i<8;i++){
            cartas[i]=new JButton("*********");
            cartas[i].addActionListener(this);
        cartas[i].setBackground(Color.WHITE);
            panelCentral.add(cartas[i]);
        }
    }


    public void actionPerformed(ActionEvent e) {


        JButton boton=(JButton) e.getSource();
        String[] figuras=new String[8];
        figuras[0]="♥";
        figuras[1]="♣";
        figuras[2]="♠";
        figuras[3]="♦";

        if(boton == juegoNuevo){ 
            for(int i=0;i<8;i++){

            cartas[i].setEnabled(true); cartas[i].setLabel("*********");
                 cartas[i].setBackground(Color.CYAN);
               }

            marcador.setText("Juego nuevo"); 
            juegoNuevo.setEnabled(false);
            return; 

        }
        int random = new Random().nextInt(3)+1;
            if(boton == cartas[0]){
                    cartas[0].setLabel(figuras[random]);
                }
                    if(boton == cartas[1]){
                    int numAleatorio=(int) (Math.random()*4);
                            cartas[1].setLabel(figuras[random]);
                        }
                    if(boton == cartas[2]){
                        int numAleatorio=(int) (Math.random()*4);
                                cartas[2].setLabel(figuras[numAleatorio]);
                            }
                    if(boton == cartas[3]){
                        int numAleatorio=(int) (Math.random()*4);
                                cartas[3].setLabel(figuras[numAleatorio]);
                            }
                    if(boton == cartas[4]){
                        int numAleatorio=(int) (Math.random()*4);
                                cartas[4].setLabel(figuras[numAleatorio]);
                            }
                    if(boton == cartas[5]){
                        int numAleatorio=(int) (Math.random()*4);
                                cartas[5].setLabel(figuras[numAleatorio]);
                            }
                    if(boton == cartas[6]){
                        int numAleatorio=(int) (Math.random()*4);
                                cartas[6].setLabel(figuras[numAleatorio]);
                            }
                    if(boton == cartas[7]){
                        int numAleatorio=(int) (Math.random()*4);
                                cartas[7].setLabel(figuras[numAleatorio]);
                            }
    }
    }

最佳答案

正如 cricket_007 所说,我的解释是您想要将值存储在变量中。在这种情况下,只需使用这样的代码 int random = new Random().nextInt(x)+1 其中 x 是您想要的范围(例如 1 到 100 将是 Random .nextInt(100)+1)

关于java - 如何选择一个随机数并保存它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36879681/

相关文章:

java - 需要使用数组列表中的点绘制路径

java - 关于仅从方法调用返回相同值的问题

java - 对于具有一种实现的接口(interface),我如何才能直接进入该实现?

java - Eclipse View 中的 GridBagLayout

java - 创建类路径资源中定义的名称为 'processEngine' 的 bean 时出错

java - 房间无异常错误。错误: Not sure how to handle query method's return type

java - "Error type 3 Error: Activity class {} does not exist",启动 Activity 时错误类型 3 Activity 类不存在

java - JSP解析时出错

java - 如何使用 Selenium Hub、Node 和 Remote Webdriver 捕获图像并将其附加到 Cucumber Reports (Jenkins)?

java - Java 开发的最佳 Eclipse 版本