gpt4 book ai didi

java - 计算石头、剪刀、布的获胜次数

转载 作者:行者123 更新时间:2023-12-02 07:11:13 26 4
gpt4 key购买 nike

我有点困惑如何创建一个字段,其中将显示您获胜或平局的次数。喜欢:

| 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());
}
}




}

最佳答案

添加全局变量:

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

关于java - 计算石头、剪刀、布的获胜次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15503732/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com