import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.io.*;
public class testframe extends Frame implements ActionListener
{
Button b1;
Panel p1;
Label text1;
TextField text2;
TextArea text3;
int num[];
int i;
String str;
int times=0;
int A,B; public testframe(String str)
{
b1=new Button("确定");
p1=new Panel();
text1=new Label("输入四位整数:");
text2=new TextField(10);
text3=new TextArea(10,10);
p1.setLayout(null);
p1.setBackground(Color.GRAY);
setSize(300,300);
setVisible(true);
b1.setBackground(Color.BLUE);
b1.setForeground(Color.WHITE);
b1.setSize(80,30);
p1.add(text1);
p1.add(text2);
p1.add(text3);
p1.add(b1);
text1.setBounds(0,0,100,20);
text2.setBounds(0,2*text1.getBounds().height,150,20);
text3.setBounds(0,3*text1.getBounds().height,150,150);
b1.setBounds(200,50,60,30);
add(p1);
b1.addActionListener(this);
addWindowListener(new shut());
}
public void rand()
{ num=new int[10];
for(i=0;i<4;i++)
{
num[i]=(int)(Math.random()*10);
int j=0;
while(j<i)
{if(num[j]==num[i])
{num[i]=(int)(Math.random()*10);
continue;
}
j++;
}
}
}public void compair()
{
int x,m,n;
x=Integer.parseInt(text2.getText());
A=0;B=0;
for(m=3;m>-1;m--){
for(n=0;n<4;n++){ if((int)(x/Math.pow(10,m))==num[n])
{
if(m+n==3)
A++;
else
B++;
} }
x=x-(int)(x/Math.pow(10,m))*(int)(Math.pow(10,m));
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
if(times<5){
if(Integer.parseInt(text2.getText())<1000||Integer.parseInt(text2.getText())>9999)
text2.setText("输入数字不合法");
else
{ if(A==4)
text2.setText("猜对了"); times++; compair();
text3.append("第");
text3.append(String.valueOf(times));
text3.append("猜的结果是:");
text3.append(String.valueOf(A));text3.append("A");
text3.append(String.valueOf(B));text3.append("B");
text3.append("\n");
}
}
else
{ text3.setText("超过5次,重新开始.\n");
times=0;
rand();
}
}
public static void main(String[] args)
{
new testframe("窗口程序");
}
}
class shut extends WindowAdapter {
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
import java.awt.event.*;
import java.applet.*;
import java.io.*;
public class testframe extends Frame implements ActionListener
{
Button b1;
Panel p1;
Label text1;
TextField text2;
TextArea text3;
int num[];
int i;
String str;
int times=0;
int A,B; public testframe(String str)
{
b1=new Button("确定");
p1=new Panel();
text1=new Label("输入四位整数:");
text2=new TextField(10);
text3=new TextArea(10,10);
p1.setLayout(null);
p1.setBackground(Color.GRAY);
setSize(300,300);
setVisible(true);
b1.setBackground(Color.BLUE);
b1.setForeground(Color.WHITE);
b1.setSize(80,30);
p1.add(text1);
p1.add(text2);
p1.add(text3);
p1.add(b1);
text1.setBounds(0,0,100,20);
text2.setBounds(0,2*text1.getBounds().height,150,20);
text3.setBounds(0,3*text1.getBounds().height,150,150);
b1.setBounds(200,50,60,30);
add(p1);
b1.addActionListener(this);
addWindowListener(new shut());
}
public void rand()
{ num=new int[10];
for(i=0;i<4;i++)
{
num[i]=(int)(Math.random()*10);
int j=0;
while(j<i)
{if(num[j]==num[i])
{num[i]=(int)(Math.random()*10);
continue;
}
j++;
}
}
}public void compair()
{
int x,m,n;
x=Integer.parseInt(text2.getText());
A=0;B=0;
for(m=3;m>-1;m--){
for(n=0;n<4;n++){ if((int)(x/Math.pow(10,m))==num[n])
{
if(m+n==3)
A++;
else
B++;
} }
x=x-(int)(x/Math.pow(10,m))*(int)(Math.pow(10,m));
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
if(times<5){
if(Integer.parseInt(text2.getText())<1000||Integer.parseInt(text2.getText())>9999)
text2.setText("输入数字不合法");
else
{ if(A==4)
text2.setText("猜对了"); times++; compair();
text3.append("第");
text3.append(String.valueOf(times));
text3.append("猜的结果是:");
text3.append(String.valueOf(A));text3.append("A");
text3.append(String.valueOf(B));text3.append("B");
text3.append("\n");
}
}
else
{ text3.setText("超过5次,重新开始.\n");
times=0;
rand();
}
}
public static void main(String[] args)
{
new testframe("窗口程序");
}
}
class shut extends WindowAdapter {
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
有问题大家互相关照!
你最重要的问题在于运行compair()之前没有运行randam(),而给数组的赋值却放在了randam()里,函数执行顺序不对。
import java.awt.event.*;public class Test extends Frame implements ActionListener{
Button b1;
Panel p1;
Label text1;
TextField text2;
TextArea text3;
int num[];
int i;
String str;
int times=0;
int A,B;
public Test(String str){
b1=new Button("确定");
p1=new Panel();
text1=new Label("输入四位整数:");
text2=new TextField(10);
text3=new TextArea(10,10);
p1.setLayout(null);
p1.setBackground(Color.GRAY);
setSize(300,300);
setVisible(true);
b1.setBackground(Color.BLUE);
b1.setForeground(Color.WHITE);
b1.setSize(80,30);
p1.add(text1);
p1.add(text2);
p1.add(text3);
p1.add(b1);
text1.setBounds(0,0,100,20);
text2.setBounds(0,2*text1.getBounds().height,150,20);
text3.setBounds(0,3*text1.getBounds().height,150,150);
b1.setBounds(200,50,60,30);
add(p1);
b1.addActionListener(this);
addWindowListener(new Shut());
}
//产生随机数4位,赋给数组num
public void randam(){
num=new int[5];
for(i=0;i<4;i++){
num[i]=(int)(Math.random()*10);
int j=0;
while(j<i){
if(num[j]==num[i]){
num[i]=(int)(Math.random()*10);
continue;
}
j++;
}
}
}
//比较输入数与num中是否一致
public void compair(){
int x;
x=Integer.parseInt(text2.getText());
A=0;B=0;
for(int m=3;m>-1;m--){
for(int n=0;n<4;n++){
if((int)(x/Math.pow(10,m))==num[n])
{
if(m+n==3)
A++;
else
B++;
}
}
x=x-(int)(x/Math.pow(10,m))*(int)(Math.pow(10,m));
}
} public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
if(times<5){
if(Integer.parseInt(text2.getText())<1000||Integer.parseInt(text2.getText())>9999)
text2.setText("输入数字不合法");
else{
randam();
compair();
if(A==4)text2.setText("猜对了");
times++;
text3.append("第");
text3.append(String.valueOf(times));
text3.append("猜的结果是:");
text3.append(String.valueOf(A));text3.append(A+"");
text3.append(String.valueOf(B));text3.append(B+"");
text3.append("\n");
}
}else{
text3.setText("超过5次,重新开始.\n");
times=0; }
}
class Shut extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
public static void main(String[] args){
new Test("窗口程序");
}
}