java GUI猜数字游戏程序
发布网友
发布时间:2022-06-01 02:26
我来回答
共3个回答
热心网友
时间:2023-11-12 20:50
package j;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
@SuppressWarnings("serial")
public class J<button> extends JFrame
{
JButton bRule;
JTextField Dnumber;
JButton bSure;
JButton bSurrender;
JButton bEnd;
JTextArea Dreasult;
int[] guessArray;
int[] randomNumber;
String sstr;
String reasult;
int x, y;
int n;
public J() //构造函数
{ //函数的声明
makenames();
layoutComponents();
setTitle("数字游戏");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
makenumber();
register();
}
//计时开始:
long startTime = System.currentTimeMillis();
long endTime;
public void makenames()
{
Dnumber = new JTextField(5);
Dreasult = new JTextArea();
bRule=new JButton("游戏规则");
bSure = new JButton("确定");
bSurrender=new JButton("投降");
bEnd=new JButton("结束");
reasult = new String("");
n = 0;
}
public void layoutComponents()
{
Container contentPane = this.getContentPane();
contentPane.setLayout(new GridLayout(4, 1));
JPanel Panel = new JPanel();
Panel.add(bRule);
bRule.setForeground(Color.red);
bRule.setFont(new Font("宋体",Font.BOLD,25));
JPanel Pane2 = new JPanel();
Pane2 .add(Dnumber);
Pane2.add(bSure);
JPanel Pane4 = new JPanel();
Pane4.add(bSurrender);
Pane4.add(bEnd);
JScrollPane scrollPane = new JScrollPane(Dreasult);
contentPane.add(Panel);
contentPane.add(Pane2);
contentPane.add(Pane4);
contentPane.add(scrollPane);
}
class bCancel implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Dnumber.setText("");
}
}
public class bRule implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(null, "系统自动生成一个4位随机数(每一位都不重复),请用户在“确定”键旁的文本框" +
"内输入一个您猜的4个数," +"\n"+
"如果猜得四位数中有位置与数值都正确的,则增加1个A;如果数值正确而位置不正确,则增加1个B。" +"\n"+
"用户猜数游戏直到用户猜对,按”结束“键结束;或用户投降,并反馈结果之后结束。",
"游戏规则", JOptionPane.PLAIN_MESSAGE);
}
}
public class bSure implements ActionListener {
boolean flag;
public void actionPerformed(ActionEvent e)
{
int m=0;
x = 0;
y = 0;
String str = new String(Dnumber.getText());
try{
int putIn = Integer.parseInt(str);
if(putIn<999||putIn>9999)
{
Dnumber.setText("");
JOptionPane.showMessageDialog(null, "必须是四个数字,请用户重新输入4位不重复的整数!",
"警告", JOptionPane.WARNING_MESSAGE);
}
guessArray = new int[4]; //把输入的一个4位数字变成数组
for (int i = 3; i >= 0; i--)
{
guessArray[0] = putIn/1000;
guessArray[1] = putIn%1000/100;
guessArray[2] = putIn%100/10;
guessArray[3] = putIn%10;
}
for(int i=0; i<4; i++) //如果有相同的数字,提示并要求重新输入
{
for(int j=i-1;j>=0;j--)
{
if(guessArray[i]==guessArray[j])
{
m++;
}
}
}
if (m>=1)
{
flag=true;
Dnumber.setText("");
JOptionPane.showMessageDialog(null, "4个数字都不能有相同,请重新输入4位不重复的整数!",
"注意", JOptionPane.PLAIN_MESSAGE);
}
}
catch(Exception e1)
{
JOptionPane.showMessageDialog(null, "输入不合法,请用户重新输入4位不重复的整数!");
Dnumber.setText("");
}
for (int i = 0; i <= 3; i++) //比较输入的与系统产生的,返回结果: xAyB
{
for (int j = 0; j <= 3; j++)
{
if (i == j)
{
if (guessArray[i] == randomNumber[j])
{
x++;
}
}
else
{
if (guessArray[i] == randomNumber[j])
{
y++;
}
}
}
}
n++;
//供编程者测试时间器:
//JOptionPane.showMessageDialog(null, sstr, "提示", JOptionPane.PLAIN_MESSAGE);
String a;
if (x == 4)
{
a = "第"+ n +"次" + " " + str + " " +x + "A" +
y +"B" + "\n" + "You are right!" + "\n"+sstr;
endTime=System.currentTimeMillis(); //计时结束
JOptionPane.showMessageDialog(null,"您总共猜了"+n+"次,共花了"+(endTime-startTime)/1000+
"秒的时间答对正确答案!");
}
else
{
a = "第"+ n +"次数" + " " + str + " " +x + "A" +
y +"B" + "\n";
}
Dreasult.setText(reasult+a);
Dnumber.setText("");
}
}
public class bSurrender implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String Str="您太没有耐心了!本题的正确答案为:"+sstr;
JOptionPane.showMessageDialog(null, Str,
"投降", JOptionPane.PLAIN_MESSAGE);
System.exit(0);
}
}
public class bEnd implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
public void register() //注册
{
bRule r=new bRule();
bRule.addActionListener(r);
bSure s = new bSure();
bSure.addActionListener(s);
bSurrender su = new bSurrender();
bSurrender.addActionListener(su);
bEnd end = new bEnd();
bEnd.addActionListener(end);
}
private void makenumber() //生成四位无重复的随机数
{
randomNumber = new int[4];
Set<String> s = new HashSet<String>();
Random random = new Random();
while (s.size() < 4)
{
Integer i = new Integer(random.nextInt(10));
String x = i.toString();
if (!s.contains(x))
{
s.add(x);
}
}
Iterator<String> t = s.iterator();
int ii = 0;
while (t.hasNext() && ii <= 3)
{
String x = (String) (t.next());
randomNumber[ii] = Integer.parseInt(x);
ii++;
}
sstr = s.toString();
}
public static void main(String[] args)
{
J Number = new J();
Number.setSize(new Dimension(600,350));//改变窗口大小
Number.setVisible(true);
JOptionPane.showMessageDialog(null,"请用户在左上角的对话框中输入4位不重复的整数!!");
}
}
热心网友
时间:2023-11-12 20:51
7334?
9845
0707
8745
0806
2753……………………………………………………………………………………………………………………
热心网友
时间:2023-11-12 20:51
需求能再写清楚点吗?看了半天还是搞不明白你的意思,输入的例子解释一下吧,如果分析不清楚就很难下手写程序。