import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
public class NumGuess extends JFrame implements ActionListener
{
JTextField jtfInput = new JTextField();
JButton jbOK = new JButton("确定");
JButton jbReplay = new JButton("重玩");
JButton[] jbArray = //按键数组
{
new JButton("9"),
new JButton("8"),
new JButton("7"),
new JButton("6"),
new JButton("5"),
new JButton("4"),
new JButton("3"),
new JButton("2"),
new JButton("1"),
new JButton("0")
};
JPanel jpKey = new JPanel(); //此Panel存放1~9按键
JLabel jlTips = new JLabel(); //提示标签
String answer; //这个是答案
int a,b; //A和B的个数
int count; //猜的次数不能大于8
JLabel[] jlOutput = //输出玩家猜的结果情况
{
new JLabel(""),
new JLabel(""),
new JLabel(""),
new JLabel(""),
new JLabel(""),
new JLabel(""),
new JLabel(""),
new JLabel("")
};
JPanel jpOutput = new JPanel(); //存放玩家每次猜的结果,如 5684:0A0B
public NumGuess()
{
this.setLayout(null);
this.setTitle("fifi猜数字");
jtfInput.setBounds(80,40,250,25);
this.add(jtfInput);
jlTips.setBounds(80,15,300,25);
jlTips.setText("在输入框里输入数字(4个)");
this.add(jlTips);
jbOK.setBounds(120,70,70,25);
jbOK.addActionListener(this);
this.add(jbOK);
jbReplay.setBounds(200,70,70,25);
jbReplay.addActionListener(this);
this.add(jbReplay);
jpKey.setBounds(110,90,180,140);
jpKey.setLayout(new FlowLayout());
jpKey.setBackground(new Color(129,150,180));
this.add(jpKey);
for(JButton tjb:jbArray)
{
jpKey.add(tjb);
tjb.addActionListener(this);
}
jpOutput.setBounds(160,230,95,200);
jpOutput.setLayout(new FlowLayout());
for(JLabel tjl:jlOutput)
{
jpOutput.add(tjl);
}
this.add(jpOutput);
this.setBounds(180,180,400,450);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
InitNum();
}
void InitNum()
{//初始化所猜数字
answer = "";
int a;
String ts;
Random rand = new Random();
for(int i=0;i<4;i++)
{
a = (Math.abs(rand.nextInt())%10);
ts =(new Integer(a)).toString();
for(i=0;i<answer.length();i++)
{//判断随机数是否和之前的重复
int index=answer.indexOf(ts);
if(index!=-1)
{
a = (Math.abs(rand.nextInt())%10);
ts =(new Integer(a)).toString();
}
}
answer += ts;
}
}
void compare(String input,String answer)
{//函数:比较输入字符串和答案的一致程度
this.a=0;
this.b=0;
char ci;
for(int i=0;i<input.length();i++)
{
ci=input.charAt(i);
if(ci==this.answer.charAt(i))
{
this.a++;
continue;
}
else
{
for(int j=0;j<this.answer.length();j++)
{
if(ci==this.answer.charAt(j))
{
this.b++;
break;
}
}
}
}
if(a==4)
{
jlTips.setText("恭喜你!答对了!按“重玩”键开始");
}
else
{
jlOutput[count].setText("("+(count + 1) + ") " + input + " : " + this.a + "A" + this.b + "B");
count++;
}
}
public void actionPerformed(ActionEvent e)
{//按键事件响应
for(int i=0;i<jbArray.length;i++)
{
if(e.getSource()==jbArray[i])
{
if(jtfInput.getText().length()!= 4)
{
jtfInput.setText(jtfInput.getText() + (9-i));
break;
}
else
{
jlTips.setText("只能输入4个数字!你会不会玩啊?");
break;
}
}
}
if(e.getSource()==jbOK)
{//按下确定键
String sInput;
jlTips.setText("");
if(jtfInput.getText().length()!=4)
{
jlTips.setText("请输入4个数字,看准喽");
}
else
{
if(count>=8)
{
this.jlTips.setText("都猜了8次了,哎.正确答案是:" + this.answer);
}
else
{
sInput=new String(jtfInput.getText());
this.compare(sInput,answer);
}
jtfInput.setText("");
}
}
if(e.getSource()==jbReplay)
{//按下重玩键
jtfInput.setText("");
jlTips.setText("在输入框里输入数字(4个)");
for(JLabel tjl:jlOutput)
{
tjl.setText("");
}
InitNum();
count=0;
}
}
public static void main(String args[])
{
new NumGuess();
}
}