四则运算2


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import java.util.Scanner;
import java.awt.*;

import javax.swing.*;

import java.awt.event.*;
import javax.swing.JFrame;

public class Test
{
public static void main(String [] args)
{
new MyWindow();
}

}
class MyWindow extends JFrame implements ActionListener{
JLabel L1=new JLabel("注:每题十分:");
JLabel L2=new JLabel("请输入题数(最多不超过5题):");
JTextField T1,T2,T3;
JButton B1,B2;
Random r=new Random();
int result;
double result1;
int sum=0;
int num=0;
int num1=0;
double num2=0;
int a,b;
int q,w;
double j,k;
MyWindow(){
setTitle("四则运算");
this.setSize(300,150);
T1=new JTextField(10);
T2=new JTextField(10);
T3=new JTextField(10);
B1=new JButton("确定");
B2=new JButton("确定");
T1.addActionListener(this);
T2.addActionListener(this);
B1.addActionListener(this);
B2.addActionListener(this);
setLayout(new FlowLayout(FlowLayout.CENTER,20,10));
add(L1);
add(T1);
add(L2);
add(T2);
add(B1);
add(B2);
add(T3);
setBounds(400,400,300,150);
setVisible(true);
}

public void actionPerformed(ActionEvent e) {

String str=T1.getText();
num=Integer.parseInt(str);
if(e.getSource()==B1){
for(int i=0;i<num;i++){

int m,n;
m=r.nextInt()%10;
n=m%2;
switch(n)
{
case -1:
a=r.nextInt()%100;
b=r.nextInt()%100;
T1.setText(a+"+"+"("+b+")=?");
result=a+b;
if(e.getSource()==B2){
String str1=T2.getText();
num1=Integer.parseInt(str1);
if(num1!=a+b){
T3.setText("回答错误!!!"+a+b);
}
else
{T3.setText("回答正确!!");
sum=sum+10;}
}
break;
case 0:
q=r.nextInt()%100;
w=r.nextInt()%100;
T1.setText(q+"*"+"("+w+")"+"=?");
if(e.getSource()==B2){
String str2=T2.getText();
num1=Integer.parseInt(str2);
if(num1!=q*w){
T3.setText("回答错误!!!"+q*w);
}
else
{T3.setText("回答正确!!");
sum=sum+10;}
}
break;
case 1:
j=r.nextInt()%100;
k=r.nextInt()%100;
if(k==0)
k++;
T1.setText(j+"/"+"("+k+")"+"=?");
if(e.getSource()==B2){
String str3=T2.getText();
num2=Double.parseDouble(str3);
if(num2!=(j/k)/1.00){
T3.setText("回答错误!!!"+(j/k)/1.00);
}
else
{T3.setText("回答正确!!");
sum=sum+10;}
}
break;
}

}

}
}

}

原文地址:https://www.cnblogs.com/lixin20/p/4411054.html