import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
public class GuessFigure extends JFrame
{
private JLabel picture;
private JLabel tips;
private JTextField answer;
private JButton confirm,restart;
private JTextArea youranswer;
Random generator =new Random();
int counterA,counterB,n;
int counter=0;
int []a=new int[4];
int []b=new int[4];
String array = new String();
public GuessFigure()
{
createUserInterface();
}
private void createUserInterface()
{
Container Pane = getContentPane();
Pane.setLayout(null);
picture = new JLabel();
picture.setBounds(10,10,128,128);
picture.setIcon(new ImageIcon("image/smile1.jpg"));
Pane.add(picture);
tips = new JLabel("请输入:");
tips.setBounds(10,148,59,30);
Pane.add(tips);
answer =new JTextField();
answer.setBounds(79,148,59,25);
Pane.add(answer);
confirm = new JButton("确定");
confirm.setBounds(10,198,128,30);
Pane.add(confirm);
confirm.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
confirmActionPerformed();
}
}
);
restart = new JButton("重新挑战");
restart.setBounds(10,238,128,30);
Pane.add(restart);
restart.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
restartActionPeformed();
}
}
);
youranswer = new JTextArea();
youranswer.setBounds(160,10,250,258);
youranswer.setText("猜测次数 "+"\t"+"你的结果"+"\t"+"正确情况"+"\n");
youranswer.setEditable(false);
Pane.add(youranswer);
setTitle("猜数字");
setSize(440,350);
setVisible(true);
generateRandom();
}
private void generateRandom()
{
for(int i=0;i<4;i++)
{
a[i]=generator.nextInt(9);
}
if(a[1]==a[0]) compareRandom(a[1],a[0]);
while(a[2]==a[1]||a[2]==a[0])
{
compareRandom(a[2],a[1]);
compareRandom(a[2],a[0]);
}
while(a[3]==a[2]||a[3]==a[1]||a[3]==a[0])
{
compareRandom(a[3],a[2]);
compareRandom(a[3],a[1]);
compareRandom(a[3],a[0]);
}
n=0;
}
private void compareRandom(int a,int b)
{
if (a==b)
{
a=generator.nextInt(9);
compareRandom(a,b);
}
}
private void confirmActionPerformed()
{
int temp=Integer.parseInt(answer.getText());
b[0]=temp/1000;
b[1]=temp%1000/100;
b[2]=temp%100/10;
b[3]=temp%10;
counterA=0;
counterB=0;
for(int i=0;i<4;i++)
if(a[i]==b[i]) counterA++;
else
{
for(int j=0;j<4;j++)
if((a[i]==b[j])&&(i!=j)) counterB++;
}
n++;
youranswer.append(n+"\t"+b[0]+b[1]+b[2]+b[3]+"\t"+
counterA+"A"+counterB+"B"+"\n");
answer.setText("");
if(n<=8&&counterA==4)
{
JOptionPane.showMessageDialog(null,"恭喜你!猜对了!","喜讯",JOptionPane.INFORMATION_MESSAGE);
picture.setIcon(new ImageIcon("image/smile2.jpg"));
}
if(n==8&&counterA<4)
{
picture.setIcon(new ImageIcon("image/smile3.jpg"));
JOptionPane.showMessageDialog(null,"答错了?没关系,再来一次!","GAME OVER",JOptionPane.ERROR_MESSAGE);
youranswer.append("正确答案"+"\t"+a[0]+a[1]+a[2]+a[3]);
answer.setEditable(false);
confirm.setEnabled(false);
}
}
private void restartActionPeformed()
{
youranswer.setText("猜测次数 "+"\t"+"你的结果"+"\t"+"正确情况"+"\n");
answer.setEditable(true);
confirm.setEnabled(true);
generateRandom();
picture.setIcon(new ImageIcon("image/smile1.jpg"));
}
public static void main(String args[])
{
GuessFigure application =new GuessFigure();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
}
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
public class GuessFigure extends JFrame
{
private JLabel picture;
private JLabel tips;
private JTextField answer;
private JButton confirm,restart;
private JTextArea youranswer;
Random generator =new Random();
int counterA,counterB,n;
int counter=0;
int []a=new int[4];
int []b=new int[4];
String array = new String();
public GuessFigure()
{
createUserInterface();
}
private void createUserInterface()
{
Container Pane = getContentPane();
Pane.setLayout(null);
picture = new JLabel();
picture.setBounds(10,10,128,128);
picture.setIcon(new ImageIcon("image/smile1.jpg"));
Pane.add(picture);
tips = new JLabel("请输入:");
tips.setBounds(10,148,59,30);
Pane.add(tips);
answer =new JTextField();
answer.setBounds(79,148,59,25);
Pane.add(answer);
confirm = new JButton("确定");
confirm.setBounds(10,198,128,30);
Pane.add(confirm);
confirm.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
confirmActionPerformed();
}
}
);
restart = new JButton("重新挑战");
restart.setBounds(10,238,128,30);
Pane.add(restart);
restart.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
restartActionPeformed();
}
}
);
youranswer = new JTextArea();
youranswer.setBounds(160,10,250,258);
youranswer.setText("猜测次数 "+"\t"+"你的结果"+"\t"+"正确情况"+"\n");
youranswer.setEditable(false);
Pane.add(youranswer);
setTitle("猜数字");
setSize(440,350);
setVisible(true);
generateRandom();
}
private void generateRandom()
{
for(int i=0;i<4;i++)
{
a[i]=generator.nextInt(9);
}
if(a[1]==a[0]) compareRandom(a[1],a[0]);
while(a[2]==a[1]||a[2]==a[0])
{
compareRandom(a[2],a[1]);
compareRandom(a[2],a[0]);
}
while(a[3]==a[2]||a[3]==a[1]||a[3]==a[0])
{
compareRandom(a[3],a[2]);
compareRandom(a[3],a[1]);
compareRandom(a[3],a[0]);
}
n=0;
}
private void compareRandom(int a,int b)
{
if (a==b)
{
a=generator.nextInt(9);
compareRandom(a,b);
}
}
private void confirmActionPerformed()
{
int temp=Integer.parseInt(answer.getText());
b[0]=temp/1000;
b[1]=temp%1000/100;
b[2]=temp%100/10;
b[3]=temp%10;
counterA=0;
counterB=0;
for(int i=0;i<4;i++)
if(a[i]==b[i]) counterA++;
else
{
for(int j=0;j<4;j++)
if((a[i]==b[j])&&(i!=j)) counterB++;
}
n++;
youranswer.append(n+"\t"+b[0]+b[1]+b[2]+b[3]+"\t"+
counterA+"A"+counterB+"B"+"\n");
answer.setText("");
if(n<=8&&counterA==4)
{
JOptionPane.showMessageDialog(null,"恭喜你!猜对了!","喜讯",JOptionPane.INFORMATION_MESSAGE);
picture.setIcon(new ImageIcon("image/smile2.jpg"));
}
if(n==8&&counterA<4)
{
picture.setIcon(new ImageIcon("image/smile3.jpg"));
JOptionPane.showMessageDialog(null,"答错了?没关系,再来一次!","GAME OVER",JOptionPane.ERROR_MESSAGE);
youranswer.append("正确答案"+"\t"+a[0]+a[1]+a[2]+a[3]);
answer.setEditable(false);
confirm.setEnabled(false);
}
}
private void restartActionPeformed()
{
youranswer.setText("猜测次数 "+"\t"+"你的结果"+"\t"+"正确情况"+"\n");
answer.setEditable(true);
confirm.setEnabled(true);
generateRandom();
picture.setIcon(new ImageIcon("image/smile1.jpg"));
}
public static void main(String args[])
{
GuessFigure application =new GuessFigure();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
}
假如结果是1A2B代表的是所猜的4位数中有一个数字出现在正确的位置上,有两个数字出现在错误的位置上,还有一个错误的数字。
例如:所要求猜到的数字是4567,而猜测的是4678那么结果就是1A2B,猜2345结果是0A2B。
这样够明白了么?