一、今日学习内容:
今天是第二周上课,也就是java的第二次课,今天老是讲完ppt,随即就拿出来随堂测试让我们做:
当堂测试,将四则运算随机生成100道题,实现答题判断对错并汇总功能。
【源代码】(将生成题目数改成了10道)
1 package demo;
2
3 import java.io.BufferedReader;
4 import java.io.BufferedWriter;
5 import java.io.FileReader;
6 import java.io.FileWriter;
7 import java.io.IOException;
8 import java.util.Scanner;
9
10 public class chuti {
11
12 static String[] question = new String[100];
13 static int[] answer = new int[100];
14
15 public static int getRandom(int n, int m) {
16 // 产生n->m的随机数
17 return (int) (Math.random() * (m - n) + n);
18 }
19
20 public static char getCharRandom() {
21 // 随机产生四种运算符
22 char sign = 0;
23 int Sn;
24 Sn = getRandom(1, 5);
25 switch (Sn) {
26 case 1:
27 sign = '+';
28 break;
29 case 2:
30 sign = '-';
31 break;
32 case 3:
33 sign = '×';
34 break;
35 case 4:
36 sign = '÷';
37 break;
38 }
39 return sign;
40 }
41
42 public static void main(String[] args) {
43 // TODO Auto-generated method stub
44 Scanner cin = new Scanner(System.in);
45 //File file = new File("E:\\szys.txt");
46 int i = 0;
47 int huida,score = 0;
48 do
49 {
50 int x = (int) (Math.random() * (100 - 1 )+ 1); //产生1-100的随机数
51 int y = (int) (Math.random() * (100 - 1 )+ 1); //产生1-100的随机数
52 char sign = getCharRandom();
53 /*
54 * 判断乘法的范围*/
55 switch(sign)
56 {
57 case '+':
58 question[i] = x +""+ sign +""+ y + "=";
59 huida = x + y;
60 answer[i] = huida;
61 //System.out.println( "("+ (i+1) +")"+ x + " " + sign + " " + y + "=" );
62 i++;break;
63 case '-':
64 if(x < y) //判断减数与被减数的大小关系
65 {
66 int temp;
67 temp = x;
68 x = y;
69 y = temp;
70 }
71 question[i] = x +""+ sign +""+ y + "=";
72 huida = x - y;
73 answer[i] = huida;
74 //System.out.println( "("+ (i+1) +")"+ x + " " + sign + " " + y + "=" );
75 i++;break;
76 case '×':
77 {
78 x = (int) (Math.random() * (10 - 1 )+ 1);//新生成x,y<9的随机数
79 y = (int) (Math.random() * (10 - 1 )+ 1);
80 question[i] = x +""+ sign +""+ y + "=";
81 huida = x * y;
82 answer[i] = huida;
83 //System.out.println( "("+ (i+1) +")"+ x + " " + sign + " " + y + "=" );
84 i++;
85 };break;
86 case '÷':
87 do //循环生成除法
88 {
89 y = (int) (Math.random() * (10 - 1 )+ 1);
90 x = (int) (Math.random() * (9*y - 1 )+ 1);
91
92 }
93 while(x % y != 0) ;
94 question[i] = x +""+ sign+"" + y + "=";
95 huida = x / y;
96 answer[i] = huida;
97 //System.out.println( "("+ (i+1) +")"+ x + " " + sign + " " + y + "=" );
98 i++;break;
99 }
100 }
101 while(i<10);
102
103
104 try {
105 BufferedWriter br= new BufferedWriter(new FileWriter("szys.txt"));
106 for(int k = 0;k<10;k++)
107 {
108 br.write("("+(k+1)+")"+String.valueOf(question[k])+"*"+String.valueOf(answer[k]));//读取数组进缓冲区
109 br.newLine();//写入新的一行,换行
110 br.flush();//将缓冲区存入txt
111 }
112 } catch (IOException e) {
113 //文件读写异常
114 e.printStackTrace();
115 }
116
117 try {
118 String line = null;
119 BufferedReader re = new BufferedReader(new FileReader("szys.txt"));//新定义
120 while((line = re.readLine())!= null)
121 {//逐行读取文件
122 String [] txt = line.split("\\*");//以*为分界,将.txt分开成问题和答案两块
123 System.out.println(txt[0]);//输出题目
124 System.out.print("Your answer:");
125 String an = cin.next();
126 if(txt[1].equals(an))//判断答案与回答
127 {
128 System.out.println("回答正确!");
129 score++;
130 }
131 else
132 System.out.println("回答错误!正确答案:" + txt[1]);
133 }
134 System.out.println("共答对"+ score + "题,答错" + (10-score) + "题");
135 }catch(IOException e)
136 {
137 e.printStackTrace();
138 }
139
140 }
141 }
【运行结果】

二、遇到的问题:
其实在写代码的过程中,对java语言的使用还是十分生疏的,为此,我们还是要多多益善,代码是被我们敲熟悉的,所以来说,还是要多节联系,勤加敲代码。
三、明日学习计划:
明天阅读老师让选读的书的一部分,完成九月读书心得体会。
