qq_43014392
qq_43014392
2019-06-05 23:26

做一个图形界面可以出题判断对错我将题目和答案写了方法返回字符串数组但是评分的监听器接收不到相应数据?

package suanShu;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.util.Random;
public class tuxing {

public static void main(String[] args) {
    // TODO 自动生成的方法存根
    JFrame frame=new JFrame("初级计算");//窗体名称
    JButton chuti=new JButton("出题");//出题按钮
    JButton pingfen=new JButton("评分");//评分按钮
    JLabel ques=new JLabel("请按出题按钮");//问题标签
    JLabel xinxi=new JLabel("评分信息");
    JTextField useranswer=new JTextField(10);//用户答案
    String str[]=new String[2];

    chuti.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent arg0){
            if(arg0.getSource()==chuti){//出题按钮事件监听
                    str=shuiji();
                    //str错误提示Local variable str defined in an enclosing scope must be final or effectively final
                ques.setText(str[0]);
                System.out.println(str[0]);
                System.out.println(str[1]);
            }
        }
    });
    pingfen.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent arg0){
            if(arg0.getSource()==pingfen){
                if(useranswer.getText().equals("")){
                    xinxi.setText("答案不能为空!");
                }
                System.out.println(str[1]);
                if(str[1].equals(useranswer.getText())){
                    xinxi.setText("答案正确");
                }else{
                    xinxi.setText("答案错误");
                }
            }
        }
    });

    ques.setBounds(40, 10, 100, 20);
    useranswer.setBounds(130, 10, 80,20);
    chuti.setBounds(40,40,60,20);
    pingfen.setBounds(130, 40, 60, 20);
    xinxi.setBounds(40,70,120,20);
    frame.setLayout(null);//布局管理

    frame.add(useranswer);
    frame.add(ques);
    frame.add(pingfen);
    frame.add(chuti);//添加组件
    frame.add(xinxi);

    frame.addWindowListener(new WindowAdapter(){
        public void windowClosing(WindowEvent arg0){
            System.exit(1);
        }
    });

    frame.setSize(250, 150);//设置属性
    frame.setLocation(500,300);
    frame.setVisible(true); 
}
public static String[] shuiji(){
    Random r=new Random();
    int a=0;
    int b=0;
    int d=0;
    String str[]=new String[2];
    a=r.nextInt(10);
    b=r.nextInt(10);
    d=r.nextInt(4);
    if(d==0){
        str[0]=a+" + "+b+" = ";
        int sum=a+b;
        str[1]=String.valueOf(sum);
        return str;
    }
    else if(d==1){
        do{
            a=r.nextInt(10);
            b=r.nextInt(10);
        }while(a-b<0);
        str[0]=a+" - "+b+" = ";
        int sum=a-b;
        str[1]=String.valueOf(sum);
        return str;

    }
    else if(d==2){
        str[0]=a+" * "+b+" = ";
        int sum=a*b;
        str[1]=String.valueOf(sum);
        return str;

    }
    else if(d==3){
        if(b==0){
            do{
                b=r.nextInt(10);
            }while(b==0);
            str[0]=a+" / "+b+" = ";
            int sum=a/b;
            str[1]=String.valueOf(sum);
            return str;
        }else{
            str[0]=a+" / "+b+" = ";
            int sum=a/b;
            str[1]=String.valueOf(sum);
            return str;
        }
    }
    return null;
}

}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答