2013-03-19 50 views
0

我卡住了一下如何創建一個地方將顯示多少次你贏得鬆動或繪製。像:計數岩石,紙張,剪刀贏得

| Wins: | 234 | 
| Looses:| 234 | 
| Draws: | 434 | 

意味着,如果一個新聞紙,我贏了答案加1勝量...等等...

| Wins: | 235 | 
| Looses:| 234 | 
| Draws: | 434 | 

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
public class gui extends JFrame implements ActionListener 
{ 
    public JLabel JWoL,JWoLPlayer,JWoLPC,JNumWin,JNumLose,JNumTie; 
    public static void main(String[] args) 
    { 
     gui theWindow = new gui(); 
     theWindow.show(); 
    } 
    public gui() 
    { 
     Button butRock = new Button("Rock"); 
     butRock.addActionListener(this); 
     Button butPaper = new Button("Paper"); 
     butPaper.addActionListener(this); 
     Button butScissors = new Button("Scissors"); 
     butScissors.addActionListener(this); 


     JWoLPlayer = new JLabel(); 
     JWoLPC = new JLabel(); 
     JWoL= new JLabel(); 


     JLabel rpsPlayer= new JLabel("Your Choice:"); 
     JLabel rpsComputer= new JLabel("Computers Choice:"); 
     setTitle("| RoPaS GAME |"); 


     JPanel ButtPan=new JPanel(); 
     ButtPan.setLayout(new GridLayout(1,3)); 
     ButtPan.add(butRock); 
     ButtPan.add(butPaper); 
     ButtPan.add(butScissors); 


     JPanel LabelsPan=new JPanel(); 
     LabelsPan.setLayout(new GridLayout(7,1)); 
     LabelsPan.add(rpsPlayer); 
     LabelsPan.add(JWoLPlayer); 
     LabelsPan.add(rpsComputer); 
     LabelsPan.add(JWoLPC); 



     JPanel WLPan=new JPanel(); 
     WLPan.setLayout(new BorderLayout()); 
     WLPan.add(JWoL,"Center"); 


     JPanel TwoPanesN1=new JPanel(); 
     TwoPanesN1.setLayout(new BorderLayout()); 
     TwoPanesN1.add(LabelsPan,"West"); 
     TwoPanesN1.add(WLPan,"East"); 
     getContentPane().setLayout(new GridLayout(2,1)); 
     getContentPane().add(ButtPan); 
     getContentPane().add(TwoPanesN1); 


     Font fontDisplay = new Font("Arial", Font.PLAIN, 22); 
     JWoL.setFont(fontDisplay); 
     setSize(400,200); 
     setVisible(true); 
     setResizable(false); 

     addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent ev){System.exit(0);}}); 
    } 



    public void Play(String PlayerChoice) 
    { 
     String PCchoice=PCansw(); 
     JWoLPC.setText(PCchoice); 

     if(PlayerChoice.equals(PCchoice)) 
      JWoL.setText(" Tie |"); 


     else if(PlayerChoice.equals("Rock")) 
      if(PCchoice.equals("Paper")) 
       JWoL.setText(" You Lose |"); 
      else 
       JWoL.setText(" You Win |"); 


     else if(PlayerChoice.equals("Paper")) 
      if(PCchoice.equals("Scissors")) 
       JWoL.setText(" You Lose |"); 
      else 
       JWoL.setText(" You Win |"); 


     else if(PlayerChoice.equals("Scissors")) 
      if(PCchoice.equals("Rock")) 
       JWoL.setText(" You Lose |"); 
      else 
       JWoL.setText(" You Win |"); 

    } 
    public String PCansw() 
    { 
     String rpsPC2=""; 
     int rpsPC=(int)(Math.random()*3)+1; 
     if(rpsPC==1) 
      rpsPC2= "Rock"; 
     else if(rpsPC==2) 
      rpsPC2= "Paper"; 
     else if(rpsPC==3) 
      rpsPC2= "Scissors"; 
     return rpsPC2; 
    } 
    public void actionPerformed(ActionEvent e) 
    { 
     if(e.getActionCommand().equals("Exit")) 
      System.exit(0); 
     else 
     { 
      JWoLPlayer.setText(e.getActionCommand()); 
      Play(e.getActionCommand()); 
     } 
    } 




} 
+0

我被困在如何添加一個文本字段,這將算多少贏輸或平做....... – 2013-03-19 15:33:57

+0

我知道如何做到這一點,我的意思是我可以visuali ze,但堅持如何連接... – 2013-03-19 15:39:08

回答

2

添加全局變量:

public class gui extends JFrame implements ActionListener 
{ 
    int wins = 0, losses = 0, draws = 0; 
    ... 

更換JWoL.setText(" You Lose |");recordLoss();並進行功能:

private void recordLoss() 
{ 
    JWoL.setText(" You Lose |"); 
    losses++; 
    JNumLose.setText(""+losses); 
} 

和其他的相同。

以上可能並不完全符合您的要求,或者在您的程序的其餘部分輸出感性數據,但希望能給您足夠的指導。

但我的像,而風扇:enum可能是比較正確的,但我覺得這是凌亂的Java)

public class gui extends JFrame implements ActionListener 
{ 
    static final int WINS = 0, LOSSES = 1, DRAWS = 2; 
    int[] counts = new int[3]; 
    String[] strings = {" You Win |", " You Lose |", " Tie |"}; 
    // this MUST be set up in the constructor, not here! 
    JLabel[] labels = {JNumWin, JNumLose, JNumTie}; 
    ... 

更換JWoL.setText(" You Lose |");record(LOSS);和相同的其他人,並且只有1個功能:

private void record(int type) 
{ 
    JWoL.setText(strings[type]); 
    counts[type]++; 
    labels[type].setText(""+counts[type]); 
} 
+0

如果我這樣做,它會出現語法錯誤的其他部分.... ** else ** if(PlayerChoice.equals(「Paper」)) – 2013-03-19 15:44:49

+0

@DenisNurasy用'{}'包圍它?我用更好的解決方案編輯答案。 – Dukeling 2013-03-19 15:46:04

+0

LabelsPan.add(JNumWin); \t \t LabelsPan.add(JNumLose); \t \t LabelsPan.add(JNumTie);我認爲這將是正確的答案,例如爲用戶顯示實際獲勝數,但它不起作用( – 2013-03-19 15:50:23