本次作業來源于: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、程式可以設定皮膚功能,可以改變界面的顔色。
三、結對同學及分工
姓名:陳世炯
學号:201606120006
部落格園位址:https://www.cnblogs.com/chenshijiong
擴充功能:
1)使用者答題結束以後,程式可以顯示使用者答題所用的時間;
2)使用者可以一次答題不能超過5個題目,若超過5個題目,答題結束可以顯示使用者答錯的題目個數和答對的題目個數并統計分數;
3)程式可以設定答題時間,時間設定為整數,機關為秒,若超過了答題時間未答題,則提示:時間已到。
本人擴充功能:
1)程式可以出帶括号的正整數四則運算,不支援分數,除法保留兩位小數;
2)程式可以設定皮膚功能,可以改變界面的顔色;
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)除法保留兩位小數
在做除法保留小數時,首先遇到的問題是,用java語言不知道怎麼用;解決方法是通過網上其他的部落格上參考其他人的做法,進而實作了除法的時候可以保留兩位小數。
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);
2)改變界面顔色
在做改變界面顔色初始打算是在JFrame中的一個面闆建立按鈕,直接點選按鈕可以改變窗體顔色,但開始的方法不對,需要自己一種一種顔色設定好;之後再網上看到其他的程式員用了一個方法,直接生成一個顔色選擇面闆,可以提供javaJFrame裡面所有提供的顔色。
調色代碼如下:
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);
改變窗體顔色時,點選按鈕彈出調色面闆如圖:
3)需要使用者輸入使用者名和密碼登入系統
設定好系統使用者名為:Operation,密碼為:123。可以直接登入進去,進行相應操作!
登入界面如圖:
登入成功後彈框如圖:
七、結對工作照片
八、附上源碼
源碼如下:歡迎指正
1 package SZYS;
2 import javafx.concurrent.Task;
3
4 import java.awt.*;
5 import java.awt.event.*;
6 import java.text.DecimalFormat;
7 import java.text.ParseException;
8 import java.text.SimpleDateFormat;
9 import java.util.*;
10 import java.util.Timer;
11 import javax.swing.*;
12
13 import static javafx.application.Platform.exit;
14
15 public class Operation extends JFrame{
16 public static void main(String[] args)
17 {
18 login();
19 }
20 //定義全局的分數
21 static int score=0;
22 //定義全局的value結果0
23 static float currValue;
24 // 定義全局運算式的字元串
25 static String currString;
26 //定義全局時間
27 static String currTime;
28 //定義全局題目數
29 static int currNums=0;
30
31 /**答題界面**/
32 static JLabel nameTest3=new JLabel("");
33 public static void exercise()
34 {
35 JFrame f=new JFrame();
36 f.setTitle("線上答題系統(保留兩位小數)");
37 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
38 f.setExtendedState(JFrame.MAXIMIZED_BOTH);
39 f.setVisible(true);
40 //設定視窗的大小和位置
41 f.setSize(600,500);
42 f.setLocation(400,100);
43 f.setLayout(null);
44 JPanel pan1=new JPanel();
45 JLabel name1=new JLabel("7/7=");
46 pan1.add(name1);
47 JTextField nameTest1=new JTextField(15);
48 nameTest1.setBounds(10, 10, 180, 100);
49 //nameTest1.setPreferredSize(new Dimension(180,100));
50 pan1.add(nameTest1);
51 pan1.setBounds(10, 10, 200, 120);
52 f.add(pan1);
53 JPanel pan2=new JPanel();
54 JLabel name2=new JLabel("請輸入秒數:");
55 pan2.add(name2);
56 JTextField nameTest2=new JTextField(15);
57 nameTest2.setBounds(300, 10, 180, 100);
58 // nameTest2.setPreferredSize(new Dimension(180,100));
59 pan2.add(nameTest2);
60 pan2.setBounds( 300,10, 200, 120);
61 f.add(pan2);
62
63 JPanel pan3=new JPanel();
64 pan3.setLayout(null);
65
66 nameTest3.setBounds(10, 60, 480, 200);
67 nameTest3.setPreferredSize(new Dimension(300,100));
68 pan3.add(nameTest3);
69 pan3.setBounds( 10,60, 500, 220);
70 f.add(pan3);
71 JPanel pan4 = new JPanel();
72 // pan4.setLayout(null);
73 JButton btnStart=new JButton("開始");
74 btnStart.setBounds(30, 300,30, 30);
75 btnStart.setPreferredSize(new Dimension(100,80));
76
77
78 btnStart.addActionListener(new ActionListener() {
79 @Override
80 public void actionPerformed(ActionEvent e) {
81 Map<String, Float> map = new HashMap<String, Float>();
82 map = operation();
83 Set set = map.keySet();
84 Iterator iter = set.iterator();
85 String key = (String) iter.next();
86 float value = map.get(key);
87 if (btnStart.getText().equals("開始 1")) {
88 // currNums+=1;
89 // btnStart.setText("下一題");
90 // name1.setText(key);
91 // System.out.println("value:"+value);
92 // currValue=value;
93 // currString=key;
94 // map.clear();
95 } else {
96 String ttime = nameTest2.getText();
97 int ttime3 =Integer.parseInt(ttime);
98 Timer timer = new Timer();// 執行個體化Timer類
99 timer.schedule(new TimerTask() {
100 public void run() {
101 nameTest3.setFont(new Font("宋體", Font.PLAIN, 20));
102 nameTest3.setText("你本次用時超過"+ttime3+"秒");
103 }
104 }, ttime3*1000);// 這裡百毫秒
105 btnStart.setText("下一題");
106 currNums += 1;
107 if (currNums == 5) {
108 endTime();
109 }
110
111 name1.setText(key);
112 System.out.println("value:" + value);
113 currValue = value;
114 currString = key;
115 map.clear();
116 nameTest1.addKeyListener(new KeyAdapter() {
117 public void keyPressed(KeyEvent e) {
118 //按Enter鍵執行相應操作;
119 if (e.getKeyChar() == KeyEvent.VK_ENTER) {
120 String answerStr = nameTest1.getText();
121 float answer = Float.parseFloat(answerStr);
122 //判斷正誤,進行加分,并顯示
123 System.out.println("answer:" + answer);
124 System.out.println("value:" + currValue);
125 if (answer == currValue) {
126 score += 10;
127 nameTest3.setFont(new Font("宋體", Font.PLAIN, 20));
128 nameTest3.setText("本題為:" + currString + "" + currValue + " || 您的回答正确 || 目前分數:" + score);
129 nameTest1.setText("");
130 } else {
131 nameTest3.setFont(new Font("宋體", Font.PLAIN, 20));
132 nameTest3.setText("本題為:" + currString + "" + currValue + " || 您的回答錯誤 || 目前分數:" + score);
133 }
134 }
135 }
136
137 ;
138 });
139 }
140 }
141 });
142
143 pan4.add(btnStart);
144 pan4.setBounds(40, 350, 110, 90);
145 f.add(pan4);
146 JPanel pan5 = new JPanel();
147 // pan4.setLayout(null);
148 JButton btnStart1=new JButton("計時");
149 btnStart1.setBounds(30, 300,30, 30);
150 btnStart1.setPreferredSize(new Dimension(100,80));
151 btnStart1.addActionListener(new ActionListener() {
152 @Override
153 public void actionPerformed(ActionEvent e) {
154 if(btnStart1.getText().equals("計時")){
155 btnStart1.setText("正在計時...");
156 nameTest3.setFont(new Font("宋體", Font.PLAIN, 20));
157 nameTest3.setText(" 計時開始,請認真答題");
158 //擷取目前時間
159 SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
160 currTime=df.format(new Date());
161 }
162 }
163 });
164
165 Timer timer = new Timer();// 執行個體化Timer類
166 timer.schedule(new TimerTask() {
167 public void run() {
168 }
169 }, 50000);// 這裡百毫秒
170
171
172 pan5.add(btnStart1);
173 pan5.setBounds(190, 350, 110, 90);
174 f.add(pan5);
175 JPanel pan6 = new JPanel();
176 // pan4.setLayout(null);
177 JButton btnStart2=new JButton("結束");
178 btnStart2.setBounds(30, 300,30, 30);
179 btnStart2.setPreferredSize(new Dimension(100,80));
180 btnStart2.addActionListener(new ActionListener() {
181 @Override
182 public void actionPerformed(ActionEvent e) {
183 //計算用時
184 endTime();
185 }
186 });
187
188 pan6.add(btnStart2);
189 pan6.setBounds(340, 350, 110, 90);
190 f.add(pan6);
191
192 JPanel pan7 = new JPanel();
193 JButton btnChengColor = new JButton("背景顔色");
194 btnChengColor.setBounds(30, 300,30, 30);
195 btnChengColor.setPreferredSize(new Dimension(100,80));
196 btnChengColor.addActionListener(new ActionListener() {
197 @Override
198 public void actionPerformed(ActionEvent e) {
199 Color ch = JColorChooser.showDialog(f, "顔色選擇器",
200 f.getBackground());
201
202 if (ch != null) {
203 f.getContentPane().setBackground(ch);
204 pan1.setBackground(ch);
205 pan1.repaint();
206 pan2.setBackground(ch);
207 pan2.repaint();
208 pan3.setBackground(ch);
209 pan3.repaint();
210 pan4.setBackground(ch);
211 pan4.repaint();
212 pan5.setBackground(ch);
213 pan5.repaint();
214 pan6.setBackground(ch);
215 pan6.repaint();
216 f.getContentPane().repaint();
217 }
218 }
219 });
220 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
221 f.setVisible(true);
222 pan7.add(btnChengColor);
223 pan7.setBounds(470, 350, 110, 90);
224 f.add(pan7);
225
226
227 }
228
229
230 /**生成四則運算**/
231 public static Map<String,Float> operation()
232 {
233 Map<String,Float> map=new HashMap<String, Float>();
234 String[] operators={"+","-","x","/"};
235 int x=(int)(Math.random()*100);
236 int y=(int)(Math.random()*100);
237 int z=(int)(Math.random()*100);
238 int index=(int)(Math.random()*4);
239 int index1=(int)(Math.random()*4);
240 while(index==4){
241 index=(int)Math.random()*4;
242 }
243 String operator=operators[index];
244 float result=0;
245 if(operator.equals("+"))
246 {
247 while(index1==4){
248 index1=(int)Math.random()*4;
249 }
250
251 String operator1=operators[index1];
252 if(operator1.equals("+"))
253 {
254 String formula=(x+"")+operator+(""+y)+operator1+(""+z)+"=";
255 result=x+y+z;
256 map.put(formula, result);
257 }
258 else if (operator1.equals("-"))
259 {
260 String formula=(x+"")+operator+(""+y)+operator1+(""+z)+"=";
261 result=x+y-z;
262 map.put(formula, result);
263 }
264 else if (operator1.equals("x"))
265 {
266 String formula=(x+"")+operator+(""+y)+operator1+(""+z)+"=";
267 result=x+y*z;
268 map.put(formula, result);
269 }
270 else
271 {
272 String formula=("("+x+"")+operator+(""+y)+")"+operator1+(""+z)+"=";
273 DecimalFormat df=new DecimalFormat("0.00");
274 String nums=df.format((float)(x+y)/z);
275 result=Float.parseFloat(nums);
276 map.put(formula, result);
277 }
278
279 }
280 else if(operator.equals("-"))
281 {
282 while(index1==4){
283 index1=(int)Math.random()*4;
284 }
285 String operator1=operators[index1];
286 if (operator1.equals("+"))
287 {
288 String formula=(x+"")+operator+(""+y)+operator1+(""+z)+"=";
289 result=x-y+z;
290 map.put(formula, result);
291 }
292 else if (operator1.equals("-"))
293 {
294 String formula=(x+"")+operator+(""+y)+operator1+(""+z)+"=";
295 result=x-y-z;
296 map.put(formula, result);
297 }
298 else if (operator1.equals("x"))
299 {
300 String formula=(+x+"")+operator+(""+y)+operator1+(""+z)+"=";
301 result=x-y*z;
302 map.put(formula, result);
303 }
304 else
305 {
306 String formula=("("+x+"")+operator+(""+y+")")+operator1+(""+z)+"=";
307 DecimalFormat df=new DecimalFormat("0.00");
308 String nums=df.format((float)(x-y)/z);
309 result=Float.parseFloat(nums);
310 map.put(formula, result);
311 }
312
313 }
314 else if(operator.equals("x"))
315 {
316 while(index1==4){
317 index1=(int)Math.random()*4;
318 }
319 String operator1=operators[index1];
320 if (operator1.equals("+"))
321 {
322 String formula=(x+"")+operator+(""+y)+operator1+(""+z)+"=";
323 result=x*y+z;
324 map.put(formula, result);
325 }
326 else if(operator1.equals("-"))
327 {
328 String formula=(x+"")+operator+(""+y)+operator1+(""+z)+"=";
329 result=x*y-z;
330 map.put(formula, result);
331 }
332 else if (operator1.equals("x"))
333 {
334 String formula=(x+"")+operator+(""+y)+operator1+(""+z)+"=";
335 result=x*y*z;
336 map.put(formula, result);
337 }
338 else
339 {
340 String formula=(x+"")+operator+(""+y)+operator1+(""+z)+"=";
341 DecimalFormat df=new DecimalFormat("0.00");
342 String nums=df.format((float)x*y/z);
343 result=Float.parseFloat(nums);
344 map.put(formula, result);
345 }
346
347 }
348 else
349 {
350 while(index1==4){
351 index1=(int)Math.random()*4;
352 }
353 String operator1=operators[index1];
354 if (operator1.equals("+"))
355 {
356 String formula=(x+"")+operator+(""+y)+operator1+(""+z)+"=";
357 DecimalFormat df=new DecimalFormat("0.00");
358 String nums=df.format((float)x/y+z);
359 result=Float.parseFloat(nums);
360 map.put(formula, result);
361 }
362 else if (operator.equals("-"))
363 {
364 String formula=(x+"")+operator+(""+y)+operator1+(""+z)+"=";
365 DecimalFormat df=new DecimalFormat("0.00");
366 String nums=df.format((float)x/y-z);
367 result=Float.parseFloat(nums);
368 map.put(formula, result);
369 }
370 else if (operator1.equals("x"))
371 {
372 String formula=(x+"")+operator+(""+y)+operator1+(""+z)+"=";
373 DecimalFormat df=new DecimalFormat("0.00");
374 String nums=df.format((float)x/y*z);
375 result=Float.parseFloat(nums);
376 map.put(formula, result);
377 }
378 else
379 {
380 String formula=(x+"")+operator+(""+y)+operator1+(""+z)+"=";
381 DecimalFormat df=new DecimalFormat("0.00");
382 String nums=df.format((float)x/y/z);
383 result=Float.parseFloat(nums);
384 map.put(formula, result);
385 }
386
387 }
388 return map;
389 }
390 /**登入跳轉方法**/
391 public static void login()
392 {
393 JFrame f=new JFrame();
394 f.setTitle("系統登入界面");
395 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
396 f.setExtendedState(JFrame.MAXIMIZED_BOTH);
397 f.setVisible(true);
398 //設定視窗的大小和位置
399 f.setSize(400,400);
400 f.setLocation(420,120);
401 Container con=f.getContentPane();//生成一個容器
402 con.setLayout(new GridLayout(7,1));
403 //生成一個新的版面
404 JPanel pan1=new JPanel();
405 JLabel title=new JLabel("歡迎登陸本系統");
406 title.setFont(new Font("宋體",Font.BOLD, 20));
407 pan1.add(title);
408 con.add(pan1);
409 //最上面的登陸文字
410 //生成一個新的版面
411
412 JPanel pan2=new JPanel();
413 JLabel name=new JLabel("使用者名");
414 pan2.add(name);
415 JTextField nameTest=new JTextField(15);
416 pan2.add(nameTest);
417 con.add(pan2);
418 f.setSize(500,600);
419 //使用者名及其文本框放置在第二個版面上
420
421 //生成一個新的版面
422 JPanel pan3=new JPanel();
423 JLabel pass = new JLabel("密碼");
424 pan3.add(pass);
425 JPasswordField password=new JPasswordField(15);
426 password.setEchoChar('*');
427 pan3.add(password);
428 con.add(pan3);
429 //密碼及其密碼域放在第三個版面上
430 // System.out.println(username+" "+userPassword);
431 JPanel pan4 = new JPanel();
432 JButton b_log=new JButton("登陸");
433 b_log.addActionListener(new ActionListener() {
434 public void actionPerformed(ActionEvent e) {
435
436 //擷取使用者名和密碼,進行校驗
437 String myUsername=nameTest.getText();
438 String myPassword=password.getText();
439 if(myUsername.equals("Operation")&&myPassword.equals("123")){
440 JOptionPane.showMessageDialog(null, "登陸成功!");
441 exercise();
442 //System.exit(0);
443 }
444 else
445 {
446 JOptionPane.showMessageDialog(null, "賬号或密碼錯誤!");
447 nameTest.setText("");
448 password.setText("");
449 }
450 }
451 });
452 pan4.add(b_log);
453 JButton b_exit=new JButton("退出");
454 pan4.add(b_exit);
455 con.add(pan4);
456 f.setSize(500,650);
457 //登陸和退出這兩個按鈕放在第四個版面上
458 JPanel pan5 = new JPanel();
459 con.add(pan5);
460 JPanel pan6 = new JPanel();
461 con.add(pan6);
462 JPanel pan7 = new JPanel();
463 con.add(pan7);
464 //空白版面
465 }
466
467 public static void endTime(){
468 SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
469 String endTime=df.format(new Date());
470 try {
471 long start =df.parse(currTime).getTime();
472 long end=df.parse(endTime).getTime();
473 int minutes = (int) ((end - start)/(1000 ));
474 nameTest3.setFont(new Font("宋體", Font.PLAIN, 20));
475 nameTest3.setText("時間:"+minutes+"秒 ||"+" 一共計算了"+currNums+"道題 || 總得分:"+score);
476 } catch (ParseException e1) {
477 e1.printStackTrace();
478 }
479 }
480 }