import java.awt.Color;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import static java.awt.Color.*;
public class ArithmeticTest7 extends JFrame implements ActionListener,KeyListener {
private final String[] str1 = {"十以内加减法", "百以内加减法", "百以内加减乘除", "千以内加减乘除","Boss挑战"};
private final String[] str2 = {"开始"};
JButton giveAnswer = new JButton("提交");//button for reset
JButton up[] = new JButton[str2.length];
JTextField display = new JTextField("");
JTextField answer = new JTextField("在这里输入答案!");
JTextField answer2 = new JTextField("");
JTextField user1 = new JTextField("姓名:");
JTextField user2 = new JTextField("");
private String name = user2.getText();
JTextArea file = new JTextArea("");
JScrollPane File = new JScrollPane(file);
JTextField numberOfDigit1 = new JTextField("年级:");
JComboBox calculate = new JComboBox();
public Font ziti = new Font("宋体", Font.BOLD, 25);
public ArithmeticTest7() throws FontFormatException,ArrayIndexOutOfBoundsException {
super(" 四则运算测试 ");
Font font = new Font("宋体", Font.BOLD, 30);
Font dadian = new Font("宋体", Font.BOLD, 75);
//super.setFont(font);
JPanel pnlHead = new JPanel(new BorderLayout());
JPanel pnlHeadUp = new JPanel(new GridLayout(1, 5));
JPanel pnlBodyEast = new JPanel(new GridLayout(2, 1));
JPanel displayArea = new JPanel(new GridLayout(1, 5));
displayArea.setBackground(lightGray);
displayArea.add(display);
//display.setBackground(lightGray);
displayArea.add(answer);
answer.setFont(font);
answer.setBackground(yellow);
answer.addKeyListener(this);
pnlHeadUp.add(user1);
pnlHeadUp.setForeground(yellow);
user1.setFont(font);
//user1.setBackground(lightGray);
pnlHeadUp.add(user2);
user2.setFont(font);
name = user2.getText();
pnlHeadUp.add(numberOfDigit1);
numberOfDigit1.setFont(font);
//numberOfDigit1.setBackground(lightGray);
pnlBodyEast.add(giveAnswer);
pnlHeadUp.add(calculate);
pnlHead.add(displayArea, BorderLayout.CENTER);
pnlHead.add(pnlHeadUp, BorderLayout.NORTH);
for (int i = 0; i < str1.length; i++) {
calculate.setFont(font);
calculate.addItem(str1[i]);
}
for (int i = 0; i < str2.length; i++) {
up[i] = new JButton(str2[i]);
up[i].setFont(font);
up[i].setBackground(Color.green);
pnlBodyEast.add(up[i]);
}
display.setFont(dadian);
giveAnswer.setFont(font);
giveAnswer.setBackground(lightGray);
//user1.setEnabled(false);
//display.setEnabled(false);
//numberOfDigit1.setEnabled(false);
JPanel pnlBody = new JPanel(new BorderLayout());
pnlBody.add(File, BorderLayout.CENTER);
pnlBody.add(pnlBodyEast, BorderLayout.NORTH);
setLayout(new BorderLayout());
add(BorderLayout.NORTH, pnlHead);
add(BorderLayout.CENTER, pnlBody);
giveAnswer.addActionListener(this);
display.addActionListener(this);
calculate.addActionListener(this);
for (int i = 0; i < str2.length; i++) {
up[i].addActionListener(this);
}
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(950, 500);
Dimension displaySize = Toolkit.getDefaultToolkit().getScreenSize(); // 获得显示器大小对象
Dimension frameSize = getSize();
setLocation((displaySize.width - frameSize.width) / 2, (displaySize.height - frameSize.height) / 2);
setVisible(true);
}
int o = 0;
int score = 0;
float[] Answer = new float[100];
int[] b = new int[100];
int[] n = new int[100];
int[] m = new int[100];
int[] k = new int[100];
String[] c = new String[20];
String N, M, K;
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
name = user2.getText();
if (source == up[0])
{
CalculateE();
answer.setFont(new Font("宋体", Font.BOLD, 75));
answer.setText("");
answer.setCaretPosition(0);
file.setText("");
score = 0;
o = 0;
}
if (source == giveAnswer) {
daan();
}
}
public void daan(){
String q = answer.getText();
float a = Float.valueOf(q);
if (Math.abs(a -Answer[o])<0.01)
score = score + 10;
String str = null;
switch (b[o]) {
case 1:
str = String.format("%-10s%-10s%-10s%-10s\r\n", n[o] + "+" + m[o] + "=", name + "答案:" + a, "标准答案:" + Answer[o], "目前得分:" + score);
break;
case 2:
if (n[o] < m[o])
str = String.format("%-10s%-10s%-10s%-10s\r\n", m[o] + "-" + n[o] + "=", name + "答案:" + a, "标准答案:" + Answer[o], "目前得分:" + score);
if (n[o] >= m[o])
str = String.format("%-10s%-10s%-10s%-10s\r\n", n[o] + "-" + m[o] + "=", name + "答案:" + a, "标准答案:" + Answer[o], "目前得分:" + score);
break;
case 3:
str = String.format("%-10s%-10s%-10s%-10s\r\n", n[o] + "*" + m[o] + "=", name + "答案:" + a, "标准答案:" + Answer[o], "目前得分:" + score);
break;
case 4:
if (n[o] < m[o]) {
str = String.format("%-10s%-10s%-10s%-10s\r\n", m[o] + "÷" + n[o] + "=", name + "答案:" + a, "标准答案:" + Answer[o], "目前得分:" + score);
}
if (n[o] >= m[o]) {
str = String.format("%-10s%-10s%-10s%-10s\r\n", n[o] + "÷" + m[o] + "=", name + "答案:" + a, "标准答案:" + Answer[o], "目前得分:" + score);
}
break;
case 5:
if (n[o] < m[o]) {
str = String.format("%-10s%-10s%-10s%-10s\r\n", m[o] + "+" + n[o] + "*" + k[o] + "=", name + "答案:" + a, "标准答案:" + Answer[o], "目前得分:" + score);
}
if (n[o] > m[o]) {
str = String.format("%-10s%-10s%-10s%-10s\r\n", n[o] + "÷" + m[o] + "-" + k[o] + "=", name + "答案:" + a, "标准答案:" + Answer[o], "目前得分:" + score);
}
}
file.setFont(ziti);
file.setText(file.getText() + str);
if (o < 10) {
o = o + 1;
if (o != 10) {
display.setText(c[o]);
answer.setText("");
answer2.setText("");
}
}
}
public void CalculateE() {
if (calculate.getSelectedItem() == "十以内加减法") {
for (int i = 0; i < 10; i++) {
b[i] = (int) (Math.random() * 2) + 1;
n[i] = (int) (Math.random() * (Math.pow(10, 1))) + 1;
m[i] = (int) (Math.random() * (Math.pow(10, 1))) + 1;
if(n[i]+m[i]>=10) i--;
}
}
if (calculate.getSelectedItem() == "百以内加减法") {
for (int i = 0; i < 10; i++) {
b[i] = (int) (Math.random() * 2) + 1;
n[i] = (int) (Math.random() * (Math.pow(10, 2))) + 1;
m[i] = (int) (Math.random() * (Math.pow(10, 2))) + 1;
}
}
if (calculate.getSelectedItem() == "百以内加减乘除") {
for (int i = 0; i < 10; i++) {
b[i] = (int) (Math.random() * 4) + 1;
n[i] = (int) (Math.random() * (Math.pow(10, 2))) + 1;
m[i] = (int) (Math.random() * (Math.pow(10, 2))) + 1;
}
}
if (calculate.getSelectedItem() == "千以内加减乘除") {
for (int i = 0; i < 10; i++) {
b[i] = (int) (Math.random() * 4) + 1;
n[i] = (int) (Math.random() * (Math.pow(10, 3))) + 1;
m[i] = ((int) (Math.random() * (Math.pow(10, 2))) + 1);
}
}
if (calculate.getSelectedItem() == "Boss挑战") {
for (int i = 0; i < 10; i++) {
b[i] = 5;
n[i] = (int) (Math.random() * (Math.pow(10, 3))) + 1;
m[i] = ((int) (Math.random() * (Math.pow(10, 2))) + 1);
k[i] = ((int) (Math.random() * (Math.pow(10, 2))) + 1);
}
}
for (int i = 0; i < 10; i++) {
N = String.valueOf(n[i]);
M = String.valueOf(m[i]);
K = String.valueOf(k[i]);
switch (b[i]) {
case 1:
c[i] = N + "+" + M + "=";
Answer[i] = n[i] + m[i];
break;
case 2:
if (n[i] < m[i])
c[i] = M + "-" + N + "=";
if (n[i] >= m[i])
c[i] = N + "-" + M + "=";
Answer[i] = Math.abs(m[i] - n[i]);
break;
case 3:
Answer[i] = n[i] * m[i];
c[i] = N + "*" + M + "=";
break;
case 4:
if (n[i] < m[i]) {
c[i] = M + "÷" + N + "=";
Answer[i] = (float)m[i] / n[i];
}
if (n[i] >= m[i]) {
c[i] = N + "÷" + M + "=";
Answer[i] =(float)n[i] / m[i];
}
break;
case 5:
if (n[i] < m[i]) {
c[i] = M + "+" + N + "*" +K + "=";
Answer[i] = (float)k[i]*n[i]+m[i];
}
if (n[i] >= m[i])
{
c[i] = N + "÷" + M + "-" + K + "=";
Answer[i] =(float)n[i] / m[i]-k[i];
}
break;
}
}
display.setText(c[0]);
}
@Override
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent k) {
if(k.getKeyChar() == KeyEvent.VK_ENTER){
daan();
}
}
@Override
public void keyReleased(KeyEvent e) {
}
public static void main(String[] args) throws FontFormatException {
ArithmeticTest7 test;
test = new ArithmeticTest7();
}
}
01-03
05-30
01-16