本次作業來源于:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/2213
GitHub庫位址:https://github.com/570048926/Software
一、本次作業要求
從個人項目出發,将程式改造成一個單機帶使用者界面(不是控制台)的程式,這個程式最基本要達到:
- 自動生成題目,單個題目最多不能超過4個運算符,操作數小于100。
- 使用者可以輸入答案
- 若使用者輸入答案正确,則提示正确;若答案錯誤,則提示錯誤,并要提示正确答案是多少。
二、本次擴充的方向
1、程式可以出帶括号的正整數四則運算,不支援分數,除法保留兩位小數,如:(1/3+1)*2 = 2.67;
2、使用者答題結束以後,程式可以顯示使用者答題所用的時間;
3、使用者可以一次答題不能超過5個題目,若超過5個題目,答題結束可以顯示使用者答錯的題目個數和答對的題目個數并統計分數;
4、使用者在答題時,需要使用者輸入使用者名 ;
5、程式可以設定答題時間,時間設定為整數,機關為秒,最大不能超過輸入時間,若超過了答題時間未答題,則提示:時間已到;
6、程式可以設定皮膚功能,可以改變界面的顔色。
三、結對同學及分工
姓名:邵警文
學号:201606120002
部落格園位址:https://home.cnblogs.com/u/ShaoJingWen/
擴充功能:
1)程式可以出帶括号的正整數四則運算,不支援分數,除法保留兩位小數;
2)程式可以設定皮膚功能,可以改變界面的顔色;
3)使用者在答題時,需要使用者輸入使用者名
本人擴充功能:
1)使用者答題結束以後,程式可以顯示使用者答題所用的時間;
2)使用者可以一次答題不能超過5個題目,若超過5個題目,答題結束可以顯示使用者答錯的題目個數和答對的題目個數并統計分數;
3)程式可以設定答題時間,時間設定為整數,機關為秒,若超過了答題時間未答題,則提示:時間已到。
四、估計所需時間
PSP2.1 | Personal Software Process Stages | Time Senior Student | Time |
Planning | 計劃 | 4 | 6 |
· Estimate | 估計這個任務需要多少時間 | 10 | 16 |
Development | 開發 | 2 | 3 |
· Analysis | 需求分析 (包括學習新技術) | ||
· Design Spec | 生成設計文檔 | 5 | |
· Design Review | 設計複審 | ||
· Coding Standard | 代碼規範 | ||
· Design | 具體設計 | 12 | |
· Coding | 具體編碼 | 24 | 36 |
· Code Review | 代碼複審 | 7 | 9 |
· Test | 測試(自我測試,修改代碼,送出修改) | ||
Reporting | 報告 | ||
· | 測試報告 | ||
計算工作量 | |||
并提出過程改進計劃 |
五、開發環境

程式設計軟體:Intellij IDEA
六、本次作業總結及感悟
1)答題結束後,顯示出其答題所用時間 ,限制其答題數不能超過5題 ,若超過5題顯示其分數及時間
JButton btnStart2=new JButton("結束");
btnStart2.setBounds(30, 300,30, 30);
btnStart2.setPreferredSize(new Dimension(100,80));
btnStart2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//計算用時
endTime();
}
});
endTime() 的功能是 把結束時間減去開始時間 計算出最後的用時時間 并把其顯示出來
public static void endTime(){
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String endTime=df.format(new Date());
try {
long start =df.parse(currTime).getTime();
long end=df.parse(endTime).getTime();
int minutes = (int) ((end - start)/(1000 ));
nameTest3.setFont(new Font("宋體", Font.PLAIN, 20));
nameTest3.setText("時間:"+minutes+"秒 ||"+" 一共計算了"+currNums+"道題 || 總得分:"+score);
} catch (ParseException e1) {
e1.printStackTrace();
}
}
2)設定其答題時間,若超過這個時間就提示其時間已到,若其答題總時間超過120秒就顯示答題時間到
Timer timer = new Timer();// 執行個體化Timer類
timer.schedule(new TimerTask() {
public void run() {
nameTest3.setFont(new Font("宋體", Font.PLAIN, 20));
nameTest3.setText("你答題已經超過120秒!");
}
}, 120000);// 這裡百毫秒
Timer timer = new Timer();// 執行個體化Timer類
timer.schedule(new TimerTask() {
public void run() {
nameTest3.setFont(new Font("宋體", Font.PLAIN, 20));
nameTest3.setText("你本次用時超過"+ttime3+"秒");
}
}, ttime3*1000);// 這裡百毫秒
btnStart.setText("下一題");
currNums += 1;
if (currNums == 5) {
endTime();
}
七、結對工作照片
八、附上源碼
源碼如下:歡迎指正
package SZYS;
import javafx.concurrent.Task;
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Timer;
import javax.swing.*;
import static javafx.application.Platform.exit;
public class Operation extends JFrame{
public static void main(String[] args)
{
login();
}
//定義全局的分數
static int score=0;
//定義全局的value結果0
static float currValue;
// 定義全局運算式的字元串
static String currString;
//定義全局時間
static String currTime;
//定義全局題目數
static int currNums=0;
/**答題界面**/
static JLabel nameTest3=new JLabel("");
public static void exercise()
{
JFrame f=new JFrame();
f.setTitle("線上答題系統(保留兩位小數)");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setExtendedState(JFrame.MAXIMIZED_BOTH);
f.setVisible(true);
//設定視窗的大小和位置
f.setSize(600,500);
f.setLocation(400,100);
f.setLayout(null);
JPanel pan1=new JPanel();
JLabel name1=new JLabel("7/7=");
pan1.add(name1);
JTextField nameTest1=new JTextField(15);
nameTest1.setBounds(10, 10, 180, 100);
//nameTest1.setPreferredSize(new Dimension(180,100));
pan1.add(nameTest1);
pan1.setBounds(10, 10, 200, 120);
f.add(pan1);
JPanel pan2=new JPanel();
JLabel name2=new JLabel("請輸入秒數:");
pan2.add(name2);
JTextField nameTest2=new JTextField(15);
nameTest2.setBounds(300, 10, 180, 100);
// nameTest2.setPreferredSize(new Dimension(180,100));
pan2.add(nameTest2);
pan2.setBounds( 300,10, 200, 120);
f.add(pan2);
JPanel pan3=new JPanel();
pan3.setLayout(null);
nameTest3.setBounds(10, 60, 480, 200);
nameTest3.setPreferredSize(new Dimension(300,100));
pan3.add(nameTest3);
pan3.setBounds( 10,60, 500, 220);
f.add(pan3);
JPanel pan4 = new JPanel();
// pan4.setLayout(null);
JButton btnStart=new JButton("開始");
btnStart.setBounds(30, 300,30, 30);
btnStart.setPreferredSize(new Dimension(100,80));
btnStart.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Map<String, Float> map = new HashMap<String, Float>();
map = operation();
Set set = map.keySet();
Iterator iter = set.iterator();
String key = (String) iter.next();
float value = map.get(key);
if (btnStart.getText().equals("開始 1")) {
// currNums+=1;
// btnStart.setText("下一題");
// name1.setText(key);
// System.out.println("value:"+value);
// currValue=value;
// currString=key;
// map.clear();
} else {
String ttime = nameTest2.getText();
int ttime3 =Integer.parseInt(ttime);
Timer timer = new Timer();// 執行個體化Timer類
timer.schedule(new TimerTask() {
public void run() {
nameTest3.setFont(new Font("宋體", Font.PLAIN, 20));
nameTest3.setText("你本次用時超過"+ttime3+"秒");
}
}, ttime3*1000);// 這裡百毫秒
btnStart.setText("下一題");
currNums += 1;
if (currNums == 5) {
endTime();
}
name1.setText(key);
System.out.println("value:" + value);
currValue = value;
currString = key;
map.clear();
nameTest1.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
//按Enter鍵執行相應操作;
if (e.getKeyChar() == KeyEvent.VK_ENTER) {
String answerStr = nameTest1.getText();
float answer = Float.parseFloat(answerStr);
//判斷正誤,進行加分,并顯示
System.out.println("answer:" + answer);
System.out.println("value:" + currValue);
if (answer == currValue) {
score += 10;
nameTest3.setFont(new Font("宋體", Font.PLAIN, 20));
nameTest3.setText("本題為:" + currString + "" + currValue + " || 您的回答正确 || 目前分數:" + score);
nameTest1.setText("");
} else {
nameTest3.setFont(new Font("宋體", Font.PLAIN, 20));
nameTest3.setText("本題為:" + currString + "" + currValue + " || 您的回答錯誤 || 目前分數:" + score);
}
}
}
;
});
}
}
});
pan4.add(btnStart);
pan4.setBounds(40, 350, 110, 90);
f.add(pan4);
JPanel pan5 = new JPanel();
// pan4.setLayout(null);
JButton btnStart1=new JButton("計時");
btnStart1.setBounds(30, 300,30, 30);
btnStart1.setPreferredSize(new Dimension(100,80));
btnStart1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(btnStart1.getText().equals("計時")){
btnStart1.setText("正在計時...");
nameTest3.setFont(new Font("宋體", Font.PLAIN, 20));
nameTest3.setText(" 計時開始,請認真答題");
//擷取目前時間
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
currTime=df.format(new Date());
}
}
});
Timer timer = new Timer();// 執行個體化Timer類
timer.schedule(new TimerTask() {
public void run() {
nameTest3.setFont(new Font("宋體", Font.PLAIN, 20));
nameTest3.setText("你答題已經超過120秒!");
}
}, 120000);// 這裡百毫秒
pan5.add(btnStart1);
pan5.setBounds(190, 350, 110, 90);
f.add(pan5);
JPanel pan6 = new JPanel();
// pan4.setLayout(null);
JButton btnStart2=new JButton("結束");
btnStart2.setBounds(30, 300,30, 30);
btnStart2.setPreferredSize(new Dimension(100,80));
btnStart2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//計算用時
endTime();
}
});
pan6.add(btnStart2);
pan6.setBounds(340, 350, 110, 90);
f.add(pan6);
JPanel pan7 = new JPanel();
JButton btnChengColor = new JButton("背景顔色");
btnChengColor.setBounds(30, 300,30, 30);
btnChengColor.setPreferredSize(new Dimension(100,80));
btnChengColor.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Color ch = JColorChooser.showDialog(f, "顔色選擇器",
f.getBackground());
if (ch != null) {
f.getContentPane().setBackground(ch);
pan1.setBackground(ch);
pan1.repaint();
pan2.setBackground(ch);
pan2.repaint();
pan3.setBackground(ch);
pan3.repaint();
pan4.setBackground(ch);
pan4.repaint();
pan5.setBackground(ch);
pan5.repaint();
pan6.setBackground(ch);
pan6.repaint();
f.getContentPane().repaint();
}
}
});
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
pan7.add(btnChengColor);
pan7.setBounds(470, 350, 110, 90);
f.add(pan7);
}
/**生成四則運算**/
public static Map<String,Float> operation()
{
Map<String,Float> map=new HashMap<String, Float>();
String[] operators={"+","-","x","/"};
int x=(int)(Math.random()*100);
int y=(int)(Math.random()*100);
int z=(int)(Math.random()*100);
int index=(int)(Math.random()*4);
int index1=(int)(Math.random()*4);
while(index==4){
index=(int)Math.random()*4;
}
String operator=operators[index];
float result=0;
if(operator.equals("+"))
{
while(index1==4){
index1=(int)Math.random()*4;
}
String operator1=operators[index1];
if(operator1.equals("+"))
{
String formula=(x+"")+operator+(""+y)+operator1+(""+z)+"=";
result=x+y+z;
map.put(formula, result);
}
else if (operator1.equals("-"))
{
String formula=(x+"")+operator+(""+y)+operator1+(""+z)+"=";
result=x+y-z;
map.put(formula, result);
}
else if (operator1.equals("x"))
{
String formula=(x+"")+operator+(""+y)+operator1+(""+z)+"=";
result=x+y*z;
map.put(formula, result);
}
else
{
String formula=("("+x+"")+operator+(""+y)+")"+operator1+(""+z)+"=";
DecimalFormat df=new DecimalFormat("0.00");
String nums=df.format((float)(x+y)/z);
result=Float.parseFloat(nums);
map.put(formula, result);
}
}
else if(operator.equals("-"))
{
while(index1==4){
index1=(int)Math.random()*4;
}
String operator1=operators[index1];
if (operator1.equals("+"))
{
String formula=(x+"")+operator+(""+y)+operator1+(""+z)+"=";
result=x-y+z;
map.put(formula, result);
}
else if (operator1.equals("-"))
{
String formula=(x+"")+operator+(""+y)+operator1+(""+z)+"=";
result=x-y-z;
map.put(formula, result);
}
else if (operator1.equals("x"))
{
String formula=(+x+"")+operator+(""+y)+operator1+(""+z)+"=";
result=x-y*z;
map.put(formula, result);
}
else
{
String formula=("("+x+"")+operator+(""+y+")")+operator1+(""+z)+"=";
DecimalFormat df=new DecimalFormat("0.00");
String nums=df.format((float)(x-y)/z);
result=Float.parseFloat(nums);
map.put(formula, result);
}
}
else if(operator.equals("x"))
{
while(index1==4){
index1=(int)Math.random()*4;
}
String operator1=operators[index1];
if (operator1.equals("+"))
{
String formula=(x+"")+operator+(""+y)+operator1+(""+z)+"=";
result=x*y+z;
map.put(formula, result);
}
else if(operator1.equals("-"))
{
String formula=(x+"")+operator+(""+y)+operator1+(""+z)+"=";
result=x*y-z;
map.put(formula, result);
}
else if (operator1.equals("x"))
{
String formula=(x+"")+operator+(""+y)+operator1+(""+z)+"=";
result=x*y*z;
map.put(formula, result);
}
else
{
String formula=(x+"")+operator+(""+y)+operator1+(""+z)+"=";
DecimalFormat df=new DecimalFormat("0.00");
String nums=df.format((float)x*y/z);
result=Float.parseFloat(nums);
map.put(formula, result);
}
}
else
{
while(index1==4){
index1=(int)Math.random()*4;
}
String operator1=operators[index1];
if (operator1.equals("+"))
{
String formula=(x+"")+operator+(""+y)+operator1+(""+z)+"=";
DecimalFormat df=new DecimalFormat("0.00");
String nums=df.format((float)x/y+z);
result=Float.parseFloat(nums);
map.put(formula, result);
}
else if (operator.equals("-"))
{
String formula=(x+"")+operator+(""+y)+operator1+(""+z)+"=";
DecimalFormat df=new DecimalFormat("0.00");
String nums=df.format((float)x/y-z);
result=Float.parseFloat(nums);
map.put(formula, result);
}
else if (operator1.equals("x"))
{
String formula=(x+"")+operator+(""+y)+operator1+(""+z)+"=";
DecimalFormat df=new DecimalFormat("0.00");
String nums=df.format((float)x/y*z);
result=Float.parseFloat(nums);
map.put(formula, result);
}
else
{
String formula=(x+"")+operator+(""+y)+operator1+(""+z)+"=";
DecimalFormat df=new DecimalFormat("0.00");
String nums=df.format((float)x/y/z);
result=Float.parseFloat(nums);
map.put(formula, result);
}
}
return map;
}
/**登入跳轉方法**/
public static void login()
{
JFrame f=new JFrame();
f.setTitle("系統登入界面");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setExtendedState(JFrame.MAXIMIZED_BOTH);
f.setVisible(true);
//設定視窗的大小和位置
f.setSize(400,400);
f.setLocation(420,120);
Container con=f.getContentPane();//生成一個容器
con.setLayout(new GridLayout(7,1));
//生成一個新的版面
JPanel pan1=new JPanel();
JLabel title=new JLabel("歡迎登陸本系統");
title.setFont(new Font("宋體",Font.BOLD, 20));
pan1.add(title);
con.add(pan1);
//最上面的登陸文字
//生成一個新的版面
JPanel pan2=new JPanel();
JLabel name=new JLabel("使用者名");
pan2.add(name);
JTextField nameTest=new JTextField(15);
pan2.add(nameTest);
con.add(pan2);
f.setSize(500,600);
//使用者名及其文本框放置在第二個版面上
//生成一個新的版面
JPanel pan3=new JPanel();
JLabel pass = new JLabel("密碼");
pan3.add(pass);
JPasswordField password=new JPasswordField(15);
password.setEchoChar('*');
pan3.add(password);
con.add(pan3);
//密碼及其密碼域放在第三個版面上
// System.out.println(username+" "+userPassword);
JPanel pan4 = new JPanel();
JButton b_log=new JButton("登陸");
b_log.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//擷取使用者名和密碼,進行校驗
String myUsername=nameTest.getText();
String myPassword=password.getText();
if(myUsername.equals("Operation")&&myPassword.equals("123")){
JOptionPane.showMessageDialog(null, "登陸成功!");
exercise();
//System.exit(0);
}
else
{
JOptionPane.showMessageDialog(null, "賬号或密碼錯誤!");
nameTest.setText("");
password.setText("");
}
}
});
pan4.add(b_log);
JButton b_exit=new JButton("退出");
pan4.add(b_exit);
con.add(pan4);
f.setSize(500,650);
//登陸和退出這兩個按鈕放在第四個版面上
JPanel pan5 = new JPanel();
con.add(pan5);
JPanel pan6 = new JPanel();
con.add(pan6);
JPanel pan7 = new JPanel();
con.add(pan7);
//空白版面
}
public static void endTime(){
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String endTime=df.format(new Date());
try {
long start =df.parse(currTime).getTime();
long end=df.parse(endTime).getTime();
int minutes = (int) ((end - start)/(1000 ));
nameTest3.setFont(new Font("宋體", Font.PLAIN, 20));
nameTest3.setText("時間:"+minutes+"秒 ||"+" 一共計算了"+currNums+"道題 || 總得分:"+score);
} catch (ParseException e1) {
e1.printStackTrace();
}
}
}