开发环境:Eclipse

结对小伙伴:201306114416 陈键 (http://www.cnblogs.com/be-the-one/

201306114452 吴舒婷 (http://www.cnblogs.com/wst-2015/

功能:

a)生成题目,单个题目最多不能超过4个运算符,操作数小于100。

    b)用户可以输入答案。

c)用户输入答案正确,正确提示;错误,则提示正确答案。

扩展方向: 

b.可以出表达式里含有负整数(负整数最小不小于-100)的题目,且负数需要带括号,用户输入的结果不用带括号。如: 2*(-4) = -8

c.用户答题结束以后,程序可以显示用户答题所用的时间

d.用户可以选择出题的个数(最多不能超过5个题目),答题结束可以显示用户答错的题目个数和答对的题目个数

e.用户在第一次答题,需要用户注册用户名,下次启动,程序记住用户前一次输入的用户名

    f.程序可以出单个整数阶乘的题目:如:4!=24

g.程序可以设置答题时间,时间设置为整数,单位为秒,最大不能超过120秒,若超过了答题时间未答题,则提示:时间已到,不能答题。(默认为120seconds,在后台倒计时)

h.程序可以设置皮肤功能,可以改变界面的颜色即可。(右键跟换皮肤)

任务分配:

我的结对同伴主要负责多一点,界面框架,线程(c,g,h)等.而我就是负责算法还有整合其他模块.So在结对时间,一般都是看着我们的驾驶员如何安全行驶.在整合的时候就会整体的审查一次,检查是否有什么不合理的代码存在.

感想:

对于收获我觉得还是挺多的,结对工作的却是能减少编程中所犯错误,程序质量也是会相对高一点.最明显的就是在这次编程中没有出现个人编辑时出现的卡在某个错误点致使不能运行的情况.事实就是这样每个人擅长的方面都是不一样的,结对工作也是结对学习的过程.在这个过程中相互提醒,通过互补来提高程序的总体质量.再者就正如书中所说,两人合作是分阶段性的,总会遇到这样的”不爽”.彼此都有自己的编程风格,但是有时候,个人觉得小如一个变量的定义也是可以决定或者说改变很多东西的.在此次合作中就碰到一次关于变量定义的邂逅.所以还是要注意重视编程的style.最后就是关于同伴之间的交流了,先是肯定partner的成果,再指出不合之处.不然个人觉得这个合作完之后应该就不能好好做朋友了.总之组队better than 单干.

import java.awt.*;
import java.awt.event.*;
import java.io.*; import javax.swing.*;
public class LayerJFrame extends JFrame {
private static final long serialVersionUID = -8620276339887152797L;
static JFrame jframe=null;
JLabel Luser=null;
int flag=0;
static JTextField Tuser=null; static JButton ok=null;
static JButton cancell=null; public void init(){
jframe=new JFrame(); Luser=new JLabel("帐号:");
Luser.setBounds(new Rectangle(40,20,70,30));
Luser.setForeground(Color.black);
Luser.setFont(new Font("楷体",20,20)); Tuser=new JTextField();
Tuser.setBounds(new Rectangle(100,20,150,30));
Tuser.setFont(new Font("楷体",20,20)); ok=new JButton("登陆");
ok.setBounds(new Rectangle(80,70,70,30));
cancell=new JButton("注册");
cancell.setBounds(new Rectangle(190,70,70,30)); jframe.add(Luser);
jframe.add(Tuser); jframe.add(ok);
jframe.add(cancell);
jframe.getContentPane().setBackground(Color.green);
jframe.setLayout(null);
jframe.setVisible(true);
jframe.setBounds(new Rectangle(450,250,330,150));
jframe.setResizable(false); ok.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
try {
String user=Tuser.getText();
BufferedReader br=new BufferedReader(new FileReader("data.txt"));
String temp=null;
do{
temp=br.readLine();
if(temp==null){
JOptionPane.showMessageDialog(jframe, "用户不存在,请先注册。");
break;
}
if(temp.equals(user))
{
flag=2;
break;
}
System.out.print(temp);
}while(temp!=null); if(flag==2){
// jframe.setVisible(false);
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Input window = new Input();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
cancell.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
try {
String user=Tuser.getText();
BufferedWriter bw=new BufferedWriter(new FileWriter("data.txt"));
bw.write(user);
bw.newLine();
JOptionPane.showMessageDialog(jframe, "用户创建成功!");
bw.flush();
bw.close();
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
}
public LayerJFrame(){
init();
}
public static void main(String[] args){
new LayerJFrame(); } }

登陆界面

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JComboBox;
import java.awt.Color;
import javax.swing.DefaultComboBoxModel;
import java.awt.GridLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import javax.swing.JLabel;
import javax.swing.JCheckBox;
import javax.swing.SwingConstants;
import javax.swing.JButton; public class Input {
public JFrame frame;
String jud1,jud2,jud3;
int number;
JComboBox comboBox ;
JCheckBox checkBox;
JCheckBox checkBox_1;
JCheckBox checkBox_2; public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Input window = new Input();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
} public Input() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(560, 150, 150, 220);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel();
frame.getContentPane().add(panel, BorderLayout.CENTER);
FlowLayout fl_panel = new FlowLayout(FlowLayout.CENTER, 5, 5);
panel.setLayout(fl_panel); JLabel lblNewLabel = new JLabel("\u9898\u76EE\u4E2A\u6570\uFF1A");
lblNewLabel.setForeground(Color.BLUE);
panel.add(lblNewLabel); comboBox = new JComboBox();
comboBox.setForeground(Color.BLUE);
comboBox.setModel(new DefaultComboBoxModel(new String[] {"1", "2", "3", "4", "5"}));
comboBox.setSelectedIndex(0);
panel.add(comboBox); checkBox = new JCheckBox("\u662F\u5426\u6709\u4E58\u9664");
checkBox.setVerticalAlignment(SwingConstants.BOTTOM);
checkBox.setForeground(Color.BLUE);
panel.add(checkBox); checkBox_1 = new JCheckBox("\u662F\u5426\u6709\u5C0F\u6570");
checkBox_1.setVerticalAlignment(SwingConstants.TOP);
checkBox_1.setForeground(Color.BLUE);
panel.add(checkBox_1); checkBox_2 = new JCheckBox("\u662F\u5426\u6709\u8D1F\u6570");
checkBox_2.setForeground(Color.BLUE);
panel.add(checkBox_2); JButton button = new JButton("\u5F00\u59CB\u505A\u9898");
button.setForeground(Color.BLUE);
panel.add(button);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
number=comboBox.getSelectedIndex()+1;
if(checkBox.isSelected())
jud1="y";
else
jud1="n";
if(checkBox_1.isSelected())
jud2="y";
else
jud2="n";
if(checkBox_2.isSelected())
jud3="y";
else
jud3="n";
new Datas(number,jud1,jud2,jud3);
//frame.setVisible(false);
}
});
} }

选择窗口

import java.util.Random;
import java.util.Scanner; public class Datas {
Random numbers;
int number;
int number2;
int up;
int down;
int score = 0;// grades
int count = 0;// rank of the pratice
String jud1, jud2, jud3; public Datas(int data,String jud1,String jud2,String jud3){
String[] qustion= new String[data];
float[] answer = new float[data];// computer's answer
float[] answer2 = new float[data];// user's answer Scanner in = new Scanner(System.in);
numbers=new Random();
number = numbers.nextInt(5)+1;
number=data;
number2 = 0;
up = 10;
down = 1;
this.jud1 =jud1; //multiply
this.jud2=jud2;//point
this.jud3 = jud3;//pessimistic for (int x = 0; x < qustion.length; x++)
qustion[x] = new String("");
do {
int XB2 = 0;// index
int XB = 0;
float num[] = new float[10];// count from 0 for numbers
String sign[] = new String[6];// count from 0 for signs
int flag = 2; // operation's length
int shuzi;
float shuzi2;
int signal = 0;// optimistic pessmistic
int b; // operator signal
int point;// pointed number
String Temp = new String("Y");
int lucknumber=numbers.nextInt(8); if(lucknumber>=2)
{
flag = (int) (Math.random() * 4) + 1;// operations' length
if (jud2.equalsIgnoreCase(Temp))// whether there is a point
{
point = (int) (Math.random() * 5);
if (jud3.equalsIgnoreCase(Temp))// pessimistic
signal = (int) (Math.random() * 5);
if (point < 3) {
shuzi2 = (float) (Math.random() * (up - down)) + down;
shuzi2 = (int) (shuzi2 * 100);
shuzi2 = shuzi2 / 100;// two points
} else
shuzi2 = (int) (Math.random() * (up - down)) + down;
if (signal < 3) {
num[XB] = shuzi2;
XB++;
qustion[number2] = String.valueOf(shuzi2);// no point
} else {
num[XB] = -shuzi2;
XB++;
qustion[number2] = "(-" + shuzi2 + ")";
}
} else {
if (jud3.equalsIgnoreCase(Temp))
signal = (int) (Math.random() * 5); shuzi = (int) (Math.random() * (up - down) + down);
if (signal < 3) {
num[XB] = shuzi;
XB++;
qustion[number2] = String.valueOf(shuzi);
} else {
num[XB] = -shuzi;
XB++;
qustion[number2] = "(-" + shuzi + ")";
}
}
// the first operator above
do {
if (jud1.equalsIgnoreCase(Temp))// whether has multiply,divide
b = (int) (Math.random() * 4 + 1);
else
b = (int) (Math.random() * 2 + 1);
switch (b) {
case 1:// add
if (jud2.equalsIgnoreCase(Temp))// little point
{
point = (int) (Math.random() * 5);
if (jud3.equalsIgnoreCase(Temp))// pessimistic
signal = (int) (Math.random() * 5);
if (point < 2) {
shuzi2 = (float) (Math.random() * (up - down))
+ down;
shuzi2 = (int) (shuzi2 * 100);
shuzi2 = shuzi2 / 100;
} else
shuzi2 = (int) (Math.random() * (up - down)) + down;
if (signal < 3) {
num[XB] = shuzi2;
XB++;
sign[XB2] = "+";
XB2++;
qustion[number2] = qustion[number2] + "+"
+ String.valueOf(shuzi2);
} else {
num[XB] = -shuzi2;
XB++;
sign[XB2] = "+";
XB2++;
qustion[number2] = qustion[number2] + "+" + "(-"
+ shuzi2 + ")";
}
} else { if (jud3.equalsIgnoreCase(Temp))// pessimistic
signal = (int) (Math.random() * 5); shuzi = (int) (Math.random() * (up - down) + down);
if (signal < 3) {
num[XB] = shuzi;
XB++;
sign[XB2] = "+";
XB2++;
qustion[number2] = qustion[number2] + "+" + shuzi;
} else {
num[XB] = -shuzi;
XB++;
sign[XB2] = "+";
XB2++;
qustion[number2] = qustion[number2] + "+" + "(-"
+ shuzi + ")";
}
}
break;
case 2:// less
if (jud2.equalsIgnoreCase(Temp)) {
point = (int) (Math.random() * 5);
if (jud3.equalsIgnoreCase(Temp))// pessimistic
signal = (int) (Math.random() * 5);
if (point < 2) {
shuzi2 = (float) (Math.random() * (up - down))
+ down;
shuzi2 = (int) (shuzi2 * 100);
shuzi2 = shuzi2 / 100;
} else
shuzi2 = (int) (Math.random() * (up - down)) + down;
if (signal < 3) {
num[XB] = shuzi2;
XB++;
sign[XB2] = "-";
XB2++;
qustion[number2] = qustion[number2] + "-" + shuzi2;
} else {
num[XB] = -shuzi2;
XB++;
sign[XB2] = "-";
XB2++;
qustion[number2] = qustion[number2] + "-" + "(-"
+ shuzi2 + ")";
}
} else {
if (jud3.equalsIgnoreCase(Temp))// pessimistic
signal = (int) (Math.random() * 5); shuzi = (int) (Math.random() * (up - down) + down);
if (signal < 3) {
num[XB] = shuzi;
XB++;
sign[XB2] = "-";
XB2++;
qustion[number2] = qustion[number2] + "-" + shuzi;
} else {
num[XB] = -shuzi;
XB++;
sign[XB2] = "-";
XB2++;
qustion[number2] = qustion[number2] + "-" + "(-"
+ shuzi + ")";
}
}
break;
case 3:// multiply
if (jud2.equalsIgnoreCase(Temp)) {
point = (int) (Math.random() * 5);
if (jud3.equalsIgnoreCase(Temp))// pessimistic
signal = (int) (Math.random() * 5);
if (point < 2) {
shuzi2 = (float) (Math.random() * (up - down))
+ down;
shuzi2 = (int) (shuzi2 * 100);
shuzi2 = shuzi2 / 100;
} else
shuzi2 = (int) (Math.random() * (up - down)) + down;
if (signal < 3) {
num[XB - 1] = shuzi2 * num[XB - 1];
qustion[number2] = qustion[number2] + "*" + shuzi2;
} else {
num[XB - 1] = shuzi2 * num[XB - 1];
qustion[number2] = qustion[number2] + "*" + shuzi2;
}
} else {
if (jud3.equalsIgnoreCase(Temp))// pessimistic
signal = (int) (Math.random() * 5);
shuzi = (int) (Math.random() * (up - down) + down);
if (signal < 3) {
num[XB - 1] = shuzi * num[XB - 1];
qustion[number2] = qustion[number2] + "*" + shuzi;
} else {
num[XB - 1] = shuzi * num[XB - 1];
qustion[number2] = qustion[number2] + "*" + "(-"
+ shuzi + ")";
}
}
break;
case 4:// divide
if (jud2.equalsIgnoreCase(Temp)) {
point = (int) (Math.random() * 5);
if (jud3.equalsIgnoreCase(Temp))// pessimistic
signal = (int) (Math.random() * 5);
if (point < 2) {
shuzi2 = (float) (Math.random() * (up - down))
+ down;
shuzi2 = (int) (shuzi2 * 100);
shuzi2 = shuzi2 / 100;
} else
shuzi2 = (int) (Math.random() * (up - down)) + down;
if (signal < 3) {
num[XB - 1] = num[XB - 1] / shuzi2;
qustion[number2] = qustion[number2] + "÷" + shuzi2;
} else {
num[XB - 1] = num[XB - 1] / shuzi2;
qustion[number2] = qustion[number2] + "÷" + "(-"
+ shuzi2 + ")";
}
} else {
if (jud3.equalsIgnoreCase(Temp))// pessimistic
signal = (int) (Math.random() * 5); shuzi = (int) (Math.random() * (up - down) + down);
if (signal < 3) {
num[XB - 1] = num[XB - 1] / shuzi;
qustion[number2] = qustion[number2] + "÷" + shuzi;
} else {
num[XB - 1] = num[XB - 1] / shuzi;
qustion[number2] = qustion[number2] + "÷" + "(-"
+ shuzi + ")";
}
}
break;
}
flag--;
} while (flag > 0);
answer[number2] = calculation(sign, num);
}
else if(lucknumber<=1)
{
answer[number2]=1;
int lucky=(int)(Math.random()*6)+1;
qustion[number2]=String.valueOf(lucky)+"!";
for(int x=1;x<=lucky;x++)
answer[number2]=x*answer[number2];
}
number2++;
} while (number > number2); new MyJFrame(qustion,answer,number); }
// calculations
static float calculation(String a[]/* sign array */, float b[]/* operator array */) {
float answer = 0;
for (int i = 0; i < a.length; i++)
if (a[i] != null)
switch ((isoperator(a[i].charAt(0)))) {
case 1:
b[0] = b[0] + b[1];
for (int j = 1; j < b.length - 1; j++)
if (b[j + 1] != 0) {
b[j] = b[j + 1];
b[j + 1] = 0;
} else
break;
break;
case 2:
b[0] = b[0] - b[1];
for (int j = 1; j < b.length - 1; j++)
if (b[j + 1] != 0) {
b[j] = b[j + 1];
b[j + 1] = 0;
} else
break;
break;
default:
break;
}
else
break;
answer = b[0];
answer = Math.round(answer * 100);
answer = answer / 100;
return answer;// Computer answer
}
// judge whether it is a sign
static int isoperator(char c) {
if (c == '+')
return 1;
else if (c == '-')
return 2;
else if (c == '/')
return 3;
return 0;
}
}

产生算式

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Scanner;
import javax.swing.*; public class MyJFrame extends JFrame implements ActionListener, Runnable {
JLabel Exercise1, Exercise2, Exercise3, Exercise4, Exercise5;
JLabel LCorrect, LTotal;
JLabel LAnswer, Reference, Mistakes;
JLabel Ltime;
JPanel p1, p2; JTextField operation[] = new JTextField[5];
JTextField user_answer[] = new JTextField[5];
JTextField[] ComputerAnswer = new JTextField[5]; JTextField CorrectNumber;
JTextField WrongNumber;
JTextField TotalScore; JButton begin;
JButton finish;
float[] answer = new float[5];// counted,score,answer,qustion.
String[] qustion = new String[5];
String[] GainAnswer = new String[5];
int number = 0;
int Right = 0;
int Wrong = 0;
float Score = 0;
int times=0; int time=0;
int maxTime =120;
Thread r=new Thread(this);
JLabel useTime; JRadioButtonMenuItem items[];
JPopupMenu popupMenu;
Color colorValues[] = { Color.pink, Color.green, Color.yellow, Color.gray,
Color.blue, Color.CYAN }; public MyJFrame(String[] qustion, float[] answer, int number) {
this.answer = answer;
this.qustion = qustion;
this.number = number;
// GainAnswer [] is an temp to compare user's answer and computer's
// answer. this.setTitle("The four operations");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(600, 450);
this.setResizable(false); // Center setting
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screen = toolkit.getScreenSize();
int x = (screen.width - this.getWidth()) / 2;
int y = (screen.height - this.getHeight()) / 2;
this.setLocation(x, y); p1 = new JPanel();
p1.setLayout(null);
p1.setBounds(2, 100, 100, 200);
p1.setOpaque(false); Exercise1 = new JLabel("第一题:");
Exercise1.setBounds(new Rectangle(10, 50, 80, 30));
Exercise1.setForeground(Color.black);
Exercise2 = new JLabel("第二题:");
Exercise2.setBounds(new Rectangle(10, 100, 80, 30));
Exercise2.setForeground(Color.black);
Exercise3 = new JLabel("第三题:");
Exercise3.setBounds(new Rectangle(10, 150, 80, 30));
Exercise3.setForeground(Color.black);
Exercise4 = new JLabel("第四题:");
Exercise4.setBounds(new Rectangle(10, 200, 80, 30));
Exercise4.setForeground(Color.black);
Exercise5 = new JLabel("第五题:");
Exercise5.setBounds(new Rectangle(10, 250, 80, 30));
Exercise5.setForeground(Color.black);
Font set = new Font("楷体", Font.BOLD, 15);
Exercise1.setFont(set);
Exercise2.setFont(set);
Exercise3.setFont(set);
Exercise4.setFont(set);
Exercise5.setFont(set); // the place show the operations
operation[0] = new JTextField();
operation[0].setBounds(new Rectangle(70, 50, 200, 30));
operation[0].setBackground(Color.cyan);
operation[1] = new JTextField();
operation[1].setBounds(new Rectangle(70, 100, 200, 30));
operation[1].setBackground(Color.cyan);
operation[2] = new JTextField();
operation[2].setBounds(new Rectangle(70, 150, 200, 30));
operation[2].setBackground(Color.cyan);
operation[3] = new JTextField();
operation[3].setBounds(new Rectangle(70, 200, 200, 30));
operation[3].setBackground(Color.cyan);
operation[4] = new JTextField();
operation[4].setBounds(new Rectangle(70, 250, 200, 30));
operation[4].setBackground(Color.cyan);
// user_answer's place
user_answer[0] = new JTextField();
user_answer[0].setBounds(new Rectangle(280, 50, 100, 30));
user_answer[1] = new JTextField();
user_answer[1].setBounds(new Rectangle(280, 100, 100, 30));
user_answer[2] = new JTextField();
user_answer[2].setBounds(new Rectangle(280, 150, 100, 30));
user_answer[3] = new JTextField();
user_answer[3].setBounds(new Rectangle(280, 200, 100, 30));
user_answer[4] = new JTextField();
user_answer[4].setBounds(new Rectangle(280, 250, 100, 30));
user_answer[0].setBackground(Color.cyan);
user_answer[1].setBackground(Color.cyan);
user_answer[2].setBackground(Color.cyan);
user_answer[3].setBackground(Color.cyan);
user_answer[4].setBackground(Color.cyan); if (number >= 1)
user_answer[0].addFocusListener(new FocusListener() {
public void focusGained(FocusEvent e) {
user_answer[0].setText(null);
} public void focusLost(FocusEvent e) {
}
});
if (number >= 2)
user_answer[1].addFocusListener(new FocusListener() {
public void focusGained(FocusEvent e) {
user_answer[1].setText(null);
} public void focusLost(FocusEvent e) {
}
});
if (number >= 3)
user_answer[2].addFocusListener(new FocusListener() {
public void focusGained(FocusEvent e) {
user_answer[2].setText(null);
} public void focusLost(FocusEvent e) {
}
});
if (number >= 4)
user_answer[3].addFocusListener(new FocusListener() {
public void focusGained(FocusEvent e) {
user_answer[3].setText(null);
} public void focusLost(FocusEvent e) {
}
});
if (number == 5)
user_answer[4].addFocusListener(new FocusListener() {
public void focusGained(FocusEvent e) {
user_answer[4].setText(null);
} public void focusLost(FocusEvent e) {
}
}); // the correct answers's area
ComputerAnswer[0] = new JTextField();
ComputerAnswer[0].setBounds(new Rectangle(380, 50, 100, 30));
ComputerAnswer[1] = new JTextField();
ComputerAnswer[1].setBounds(new Rectangle(380, 100, 100, 30));
ComputerAnswer[2] = new JTextField();
ComputerAnswer[2].setBounds(new Rectangle(380, 150, 100, 30));
ComputerAnswer[3] = new JTextField();
ComputerAnswer[3].setBounds(new Rectangle(380, 200, 100, 30));
ComputerAnswer[4] = new JTextField();
ComputerAnswer[4].setBounds(new Rectangle(380, 250, 100, 30));
ComputerAnswer[0].setBackground(Color.cyan);
ComputerAnswer[1].setBackground(Color.cyan);
ComputerAnswer[2].setBackground(Color.cyan);
ComputerAnswer[3].setBackground(Color.cyan);
ComputerAnswer[4].setBackground(Color.cyan); LAnswer = new JLabel("答案");
LAnswer.setBounds(new Rectangle(280, 20, 50, 30));
LAnswer.setForeground(Color.black); LAnswer.setFont(set); Reference = new JLabel("参考答案");
Reference.setBounds(new Rectangle(380, 20, 80, 30));
Reference.setForeground(Color.black);
Reference.setFont(set); Mistakes = new JLabel("错题:");
Mistakes.setBounds(new Rectangle(10, 300, 60, 30));
Mistakes.setForeground(Color.black);
Mistakes.setFont(set); LCorrect = new JLabel("对题:");
LCorrect.setBounds(new Rectangle(160, 300, 60, 30));
LCorrect.setForeground(Color.black);
LCorrect.setFont(set); LTotal = new JLabel("总分:");
LTotal.setBounds(new Rectangle(300, 300, 60, 30));
LTotal.setForeground(Color.black);
LTotal.setFont(set);
// button
begin = new JButton("Start/Restart");
begin.setBounds(new Rectangle(100,350,110,30));
begin.setForeground(Color.black);
begin.addActionListener(this);
begin.setToolTipText("Begin or Restart your operations."); finish = new JButton("Finish");
finish.setBounds(new Rectangle(280, 350, 100, 30));
finish.setForeground(Color.black);
finish.addActionListener(this);
finish.setToolTipText("Check your answer."); WrongNumber = new JTextField();
WrongNumber.setBounds(new Rectangle(50, 300, 80, 30));
WrongNumber.setBackground(Color.red); CorrectNumber = new JTextField();
CorrectNumber.setBounds(new Rectangle(200, 300, 80, 30));
CorrectNumber.setBackground(Color.cyan); TotalScore = new JTextField();
TotalScore.setBounds(new Rectangle(340, 300, 80, 30));
TotalScore.setBackground(Color.green); Font set2 = new Font("楷体", Font.BOLD, 20);
Ltime = new JLabel(" 右键更换背景颜色 ");
Ltime.setFont(set2);
Ltime.setBounds(new Rectangle(10,10, 240, 20));
Ltime.setForeground(Color.black); for (int i = 0; i < 5; i++) {
operation[i].setText("NULL");
user_answer[i].setText("Fill");
ComputerAnswer[i].setText("Wait");
} WrongNumber.setText(" NOT KNOW");
CorrectNumber.setText(" NOT KNOW");
TotalScore.setText(" NOT KNOW"); useTime = new JLabel("时间");
useTime.setBounds(new Rectangle(490, 10, 100, 20));
useTime.setForeground(Color.red);
useTime.setFont(set2); p1.add(Exercise1);
p1.add(Exercise2);
p1.add(Exercise3);
p1.add(Exercise4);
p1.add(Exercise5); p1.add(operation[0]);
p1.add(operation[1]);
p1.add(operation[2]);
p1.add(operation[3]);
p1.add(operation[4]); p1.add(user_answer[1]);
p1.add(user_answer[2]);
p1.add(user_answer[3]);
p1.add(user_answer[4]);
p1.add(user_answer[0]); p1.add(ComputerAnswer[0]);
p1.add(ComputerAnswer[1]);
p1.add(ComputerAnswer[2]);
p1.add(ComputerAnswer[3]);
p1.add(ComputerAnswer[4]); p1.add(LTotal);
p1.add(TotalScore);
p1.add(LCorrect);
p1.add(CorrectNumber);
p1.add(begin);
p1.add(finish);
p1.add(Mistakes);
p1.add(WrongNumber);
p1.add(Reference);
p1.add(LAnswer);
p1.add(Ltime); p1.add(useTime);
this.add(p1);
this.setVisible(true); r.start();
r.suspend();//挂起 // setting background colors
popupMenu = new JPopupMenu();
ButtonGroup colorGroup = new ButtonGroup();
ItemHandler handler = new ItemHandler(); String colors[] = { "pink", "green", "yellow", "gray", "bule", "CYAN" };
items = new JRadioButtonMenuItem[colors.length];
// 创建弹出式菜单中的各个菜单项,并给每一个菜单项注册事件监听器 for (int count = 0; count < items.length; count++) {
items[count] = new JRadioButtonMenuItem(colors[count]);
popupMenu.add(items[count]);
colorGroup.add(items[count]);
items[count].addActionListener(handler);
}
// background color
getContentPane().setBackground(Color.white);
this.addMouseListener(// 向主窗体注册监听器
new MouseAdapter() {
public void mousePressed(MouseEvent event) {
checkForTriggerEvent(event);
} public void mouseReleased(MouseEvent event) {
checkForTriggerEvent(event);
} // 检查MouseEvent事件,看其是否是弹出式菜单触发器
private void checkForTriggerEvent(MouseEvent event) {
if (event.isPopupTrigger()) {
popupMenu.show(event.getComponent(), event.getX(),
event.getY());
}
}
});
} // 菜单项选中时的事件处理
private class ItemHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
int i;
for (i = 0; i < items.length; i++) {
if (e.getSource() == items[i]) {
getContentPane().setBackground(colorValues[i]);
}
}
}
} public void run() {
boolean stop=false;
while(!stop){
try{
Thread.sleep(1000);
}catch(InterruptedException ex){
}
time++;
useTime.setText("用时"+String.valueOf(time)+"秒");
if(maxTime==time){
stop=true;
JOptionPane.showMessageDialog(this, "超过答题时间,请停止答题!");
}
}
} // ways
public void actionPerformed(ActionEvent e) {
int flag = 1;
if (e.getSource() == begin) {
r.resume();
for (int i = 0; i < qustion.length; i++) {
operation[i].setText(qustion[i]);
}
times++;
if(times>1)
{
r.suspend();//挂起
this.setVisible(false);
}
if(times>1){
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Input window = new Input();
window.frame.setVisible(true); } catch (Exception e) {
e.printStackTrace();
}
}
}); }
} if (e.getSource() == finish) {
for (int j = 0; j < number; j++)
GainAnswer[j] = user_answer[j].getText(); for (int j = 0; j < number; j++)
if (GainAnswer[j].isEmpty() || GainAnswer[j].equals("Fill")) {
flag = 0;
JOptionPane.showMessageDialog(this, "You didn't Finish !");
break;
} if (flag == 1) {
for (int x = 0; x < number; x++) {
if (answer[x] == changeanswer(GainAnswer[x])) {
Right++;
ComputerAnswer[x].setText("Right");
} else {
ComputerAnswer[x].setText(String.valueOf(answer[x]));
}
} Wrong = (int) (((1.0 * Right) / number) * 10000);
Score = Wrong / 100;
TotalScore.setText(String.valueOf(Score));
CorrectNumber.setText(String.valueOf(Right));
WrongNumber.setText(String.valueOf(number - Right));
r.suspend();
}
}
} // 是否是符号
static int isoperator(char c) {
if (c == '+')
return 1;
else if (c == '-')
return 2;
else if (c == '/')// 为了分数的答案做准备。
return 3;
return 0;
} static float changeanswer(String a) {// 用户输入的答案
float Ranswer = 0;
int i;
float temp[] = new float[2];
String apart[] = new String[2];
for (i = 0; i < a.length(); i++)
if (isoperator(a.charAt(i)) == 3)
break;
if (i == a.length())
Ranswer = Float.parseFloat(a);
else {
apart = a.split("/");
temp[0] = Float.parseFloat(apart[0].toString());
temp[1] = Float.parseFloat(apart[1].toString());
Ranswer = temp[0] / temp[1];
Ranswer = Math.round(Ranswer * 100);
Ranswer = Ranswer / 100;
}
return Ranswer;
}
}

用户做题

Four-Operations的更多相关文章

  1. backup, file manipulation operations (such as ALTER DATABASE ADD FILE) and encryption changes on a database must be serialized.

    昨天在检查YourSQLDba备份时,发现有台数据库做备份时出现了下面错误信息,如下所示: <Exec>   <ctx>yMaint.ShrinkLog</ctx> ...

  2. HDU 5938 Four Operations(四则运算)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  3. ios基础篇(二十九)—— 多线程(Thread、Cocoa operations和GCD)

    一.进程与线程 1.进程 进程是指在系统中正在运行的一个应用程序,每个进程之间是独立的,每个进程均运行在其专用且受保护的内存空间内: 如果我们把CPU比作一个工厂,那么进程就好比工厂的车间,一个工厂有 ...

  4. OpenCascade Modeling Algorithms Boolean Operations

    Modeling Algorithms Boolean Operations of Opencascade eryar@163.com 布尔操作(Boolean Operations)是通过两个形状( ...

  5. A.Kaw矩阵代数初步学习笔记 4. Unary Matrix Operations

    “矩阵代数初步”(Introduction to MATRIX ALGEBRA)课程由Prof. A.K.Kaw(University of South Florida)设计并讲授. PDF格式学习笔 ...

  6. A.Kaw矩阵代数初步学习笔记 3. Binary Matrix Operations

    “矩阵代数初步”(Introduction to MATRIX ALGEBRA)课程由Prof. A.K.Kaw(University of South Florida)设计并讲授. PDF格式学习笔 ...

  7. mouse scrollings and zooming operations in linux & windows are opposite

    mouse scrollings and zooming operations in linux & windows are opposite. windows中, 鼠标滚动的方向是: 查看页 ...

  8. MongoDB—— 写操作 Core MongoDB Operations (CRUD)

    MongoDB使用BSON文件存储在collection中,本文主要介绍MongoDB中的写操作和优化策略. 主要有三种写操作:        Create        Update        ...

  9. MongoDB—— 读操作 Core MongoDB Operations (CRUD)

    本文主要介绍内容:从MongoDB中请求数据的不同的方法 Note:All of the examples in this document use the mongo shell interface ...

  10. [codeforces 339]D. Xenia and Bit Operations

    [codeforces 339]D. Xenia and Bit Operations 试题描述 Xenia the beginner programmer has a sequence a, con ...

随机推荐

  1. Swift 并行编程现状和展望 - async/await 和参与者模式

    这篇文章不是针对当前版本 Swift 3 的,而是对预计于 2018 年发布的 Swift 5 的一些特性的猜想.如果两年后我还记得这篇文章,可能会回来更新一波.在此之前,请当作一篇对现代语言并行编程 ...

  2. (排序算法整理)NEFU 30/32

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/caihongshijie6/article/details/26165093        事实上, ...

  3. linux 的常用命令---------第九阶段

    Centos 7 系统启动及相关配置文件(面试题) 1. BIOS 初始化,开始post开机自检(主要检查磁盘.cpu.内存) 2. 加载 MBR 到内存 3. GRUB 阶段(可不说) 4. 加载内 ...

  4. WebAPI项目中使用SwaggerUI

    1.创建webapi项目解决方案 2.引入Swagger组件 在项目引用中可以看到swagger的引用 3.webapi 项目右键属性->生成-> 勾选XML文档文件,然后将XML文件保存 ...

  5. ansible 变量定义和引用

    cat /etc/ansible/hosts [nodes]10.2.1.232 key=23210.2.1.43 key=43 cat debug.yaml ---- name: test how ...

  6. Android之网络摄像头

    实现的功能就是两个手机在一个局域网内可以互相观看对方的摄像头图像,当然如果都是连接公网那么就能远程互看了,,,,和视频聊天差不多,,不过没有声音,,,,,,,, 源码是在网上找的(具体地址忘了,如有侵 ...

  7. WWDC 2015 苹果发布会 笔记

    北京时间2015年6月9日凌晨一点,美国旧金山,WWDC 2015苹果发布会,早前知道ios9会更注重系统的优化,看来果真如此,不过最让我惊讶的是Swift居然宣布开源,当时我就震惊了,不过Swift ...

  8. UWP 下载文件显示下载进度

    <Page x:Class="WgscdProject.TestDownloadPage" xmlns="http://schemas.microsoft.com/ ...

  9. WebService快速上手

    一.WebService是什么? 核心特征:跨语言.跨平台.远程调用[如果是本地系统交互,使用rpc或者com技术就行] soap:HTTP + XML [基于Http的xml格式数据交互] wsdl ...

  10. Voronoi图与Delaunay三角剖分

    详情请见[ZJOI2018]保镖 题解随笔 - 99 文章 - 0 评论 - 112