2016-04-27 76 views
0

我試圖做一個雙卡牌遊戲時,小程序啓動卡面朝下與「****」,當JButton被按下時,它會根據隨機選取的數字放一個數字,我只是想給予相同的隨機數2卡和數字並沒有改變我每次按下JButton的時間,但我不知道究竟該怎麼做如何挑選一個隨機數字並保存?

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]); 
          } 
    } 
    } 
+0

我的意思是保存它,因爲現在每次我點擊一個JButton的數字變化,我需要隨機選取一個數字,但它必須是靜態的 –

+0

如果您想在每次運行應用程序時獲得相同的一組隨機數,請爲您的隨機生成器設置一個種子。 –

+0

我的問題是,我要比較這些牌,如果這兩個牌的數字不相等,則牌翻轉,如果它們相等,他們保持正面朝上,這就是爲什麼我需要重複兩次的數字 –

回答

1

正如cricket_007說,我是什麼解釋是你想把值存儲在一個變量中。在這種情況下,只需使用這樣的代碼int random = new Random().nextInt(x)+1其中x是你想要它的範圍(例如1到100將是Random.nextInt(100)+1