qq_43014392 2019-06-05 23:26 采纳率: 0%
浏览 212

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

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;
}

}

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
    • ¥15 如何在scanpy上做差异基因和通路富集?
    • ¥20 关于#硬件工程#的问题,请各位专家解答!
    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配
    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 報錯:Person is not mapped,如何解決?