实验3—修改版
import java.awt.*;
import javax.swing.*;
import java.math.*;
import java.util.*;
import java.awt.event.*;
public class text {
public static void main(String[] args) {
new Mywindow();
}
}
class Mywindow extends JFrame{
int trueanswer=0;
int falseanswer=0;
String op;
String op2;
int answer;
int answer2=0;
int answer3;
JLabel L1=new JLabel("请输入答案:");
JLabel L2=new JLabel("答对题目:");
JLabel L3=new JLabel("错误题目:");
JLabel L4=new JLabel("题目为:");
JLabel L5=new JLabel("正确答案为:");
JButton B1=new JButton("显示题目");
JButton B2=new JButton("确定");
JButton B3=new JButton("下一题");
JTextField T1=new JTextField(5);
JTextField T2=new JTextField(5);
JTextField T3=new JTextField(5);
JTextField T4=new JTextField(10);
JTextField T5=new JTextField(10);
JTextField T6=new JTextField(10);
Mywindow(){
this.setTitle("简单的四则运算");
this.getContentPane().setBackground(Color.white);
this.setSize(200, 350);
FlowLayout flow=new FlowLayout(FlowLayout.CENTER);
this.setLayout(flow);
B1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int n=new Random().nextInt(4)+1;
int d=new Random().nextInt(2)+1;
int a=new Random().nextInt(10)+1;
int b=new Random().nextInt(10)+1;
int c=new Random().nextInt(10)+1;
int u=new Random().nextInt(4)+1;
switch(d){
case 1:
{
switch(n){
case 1:
{
op="+";
break;
}
case 2:
{
op="-";
break;
}
case 3:
{
op="*";
break;
}
case 4:
{
op="/";
break;
}
}
String str=String.valueOf(a);
String str2=String.valueOf(b);
T4.setText(str+op+str2);
break;
}
case 2:
{
switch(n){
case 1:
{
op="+";
break;
}
case 2:
{
op="-";
break;
}
case 3:
{
op="*";
break;
}
case 4:
{
op="/";
break;
}
}
switch(u){
case 1:
{
op2="+";
break;
}
case 2:
{
op2="-";
break;
}
case 3:
{
op2="*";
break;
}
case 4:
{
op2="/";
break;
}
}
String str=String.valueOf(a);
String str2=String.valueOf(b);
String str3=String.valueOf(c);
T6.setText(str+op+str2+op+str3);
break;
}
}
switch(d){
case 1:
{
switch(n){
case 1:
{
answer=a+b;
break;
}
case 2:
{
answer=a-b;
break;
}
case 3:
{
answer=a*b;
break;
}
case 4:
{
answer=a/b;
break;
}
}
break;
}
case 2:
{
switch(n){
case 1:
{
answer=a+b;
break;
}
case 2:
{
answer=a-b;
break;
}
case 3:
{
answer=a*b;
break;
}
case 4:
{
answer=a/b;
break;
}
}
switch(u){
case 1:
{
answer=answer+c;
break;
}
case 2:
{
answer=answer-c;
break;
}
case 3:
{
answer=answer*c;
break;
}
case 4:
{
answer=answer/c;
break;
}
}
}
break;
}
}
});
B2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
answer2=Integer.parseInt(T1.getText());
if(answer2==answer){
trueanswer++;
}
if(answer2!=answer){
String str5=String.valueOf(answer);
T5.setText(str5);
falseanswer++;
}
String str6=String.valueOf(trueanswer);
String str7=String.valueOf(falseanswer);
T2.setText(str6);
T3.setText(str7);
T4.setText(null);
T6.setText(null);
}
});
add(L4);add(T4);add(T6);add(L1);add(T1);add(L5);add(T5);add(L2);add(T2);add(L3);add(T3);
add(B1);add(B2);
this.setVisible(true);
}
}