2016-03-04 54 views
1

所以我嘗試做一個程序,當我點擊我的滑板或骰子按鈕時,每個標籤(代表一個骰子)都會從相應的列表中變成一個隨機元素它屬於。但到目前爲止,當我單擊按鈕時,標籤不會更改爲隨機元素,每次單擊它時都會給出相同的標籤。當我點擊按鈕時,我通過打印到系統來測試輸出,每次我都會得到相同的4個答案。我想找一些幫助,找出爲什麼它會從每個列表中重複選擇相同的元素,而不是每次都選擇不同的元素。每次都沒有選擇一個隨機項目

這是我的主類

package skateordicegui; 

import java.awt.BorderLayout; 
import java.awt.Dimension; 
import java.awt.FlowLayout; 
import java.awt.Toolkit; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 

public class SKFrame extends SkateDice { 

public JFrame jf; 

public JPanel jp; 

static public JButton sod = new JButton("SKATE OR DICE!!"); 

static public JLabel l1 = new JLabel("Stance"); 
static public JLabel l2 = new JLabel("Turn"); 
static public JLabel l3 = new JLabel("Flip"); 
static public JLabel l4 = new JLabel("Spin"); 

public SKFrame() { 
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); 

    jf = new JFrame(); 
    jf.setSize((d.width/2), (d.height/2)); 
    jf.setLocation((d.width/2 - d.width/4), (d.height/2 - d.height/4)); 
    jf.setMinimumSize(jf.getSize()); 
    jf.setResizable(true); 
    jf.pack(); 
    jf.setVisible(true); 
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    l1.setBounds(110, 150, 100, 40); 
    l2.setBounds(230, 150, 100, 40); 
    l3.setBounds(350, 150, 100,40); 
    l4.setBounds(470, 150, 100,40); 

    sod.setLocation(140, 20); 
    sod.setSize(300, 100); 

    jp = new JPanel(); 
    jp.setLayout(new FlowLayout(FlowLayout.CENTER)); 
    jp.setSize(jf.getWidth(), jf.getHeight()/2); 

    jp.add(l1); 
    jp.add(l2); 
    jp.add(l3); 
    jp.add(l4); 
    jp.add(sod); 

    jf.add(jp); 
    sod.addActionListener(action); 
} 

public ActionListener action = new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 

     Object source = e.getSource(); 
     if (source == sod) { 
     System.out.println(SkateDice.getStance()); 
     System.out.println(SkateDice.getTurn()); 
     System.out.println(SkateDice.getFlip()); 
     System.out.println(SkateDice.getSpin()); 

     l1.setText(SkateDice.getStance()); 
     l2.setText(SkateDice.getTurn()); 
     l3.setText(SkateDice.getFlip()); 
     l4.setText(SkateDice.getSpin()); 
     } else 
      System.out.println("no swag"); 
    } 
}; 



public static void main(String[] args) { 


    SKFrame skf = new SKFrame(); 

} 

} 

,這是我SkateDice類

package skateordicegui; 

import java.util.Random; 

public class SkateDice { 

static Random r = new Random(); 

// First time dealing with arrays 
//this is the easy way (kinda like python) 

static String[] d1 = {"Kickflip", "Kickflip", "Kickflip", "Heelflip", "Heelflip","Heelflip", "Shuv", "3 Shuv", "Shuv", "3 Shuv", "X", "Skate Or Dice"}; 
static String[] d2 = {"180", "180", "180", "360", "360", "360", "X", "Skate Or Dice"}; 
//OR 
static String d3[] = {"FS", "FS", "BS", "BS", "FS", "BS", "X", "Skate Or Dice"}; 
static String d4[] = {"Regular", "Regular", "Regular", "Switch", "Switch", "Switch", "Nollie", "Nollie", "Nollie", "Fakie", "Fakie", "Fakie", "X", "Skate Or Dice"}; 

public static int r1 = r.nextInt(d1.length); 
public static int r2 = r.nextInt(d2.length); 
public static int r3 = r.nextInt(d3.length); 
public static int r4 = r.nextInt(d4.length); 

public static String flip = d1[r1]; 
public static String spin = d2[r2]; 
public static String turn = d3[r3]; 
public static String stance = d4[r4]; 


static public String getFlip() { 
    return flip; 
} 

static public String getSpin() { 
    return spin; 
} 

static public String getTurn() { 
    return turn; 
} 

static public String getStance() { 
    return stance; 
} 


} 

回答

0

當你打電話給你的干將,他們只是返回值翻轉,旋轉,旋轉,姿態如預期。如果你需要他們每次打電話給他們時間來改變值,你應該實現的東西如:

static public String getFlip() { 
    // generates a new random number 
    r1 = r.nextInt(d1.length); 

    // set a new value to flip 
    flip = d1[r1]; 

    // return the value 
    return flip;    
} 

重複這種邏輯到其他getter和你的程序應該工作。

相關問題