一.计划
编写一个小学生四则运算出题程序
二.开发
1.需求分析
作为一名一年级小学生的家长,我希望制作一个出题软件,完成100以内的正整数的加减法题随机产生。以便减轻我的家庭负担。
2.生成设计文档
3.设计复审
4.代码规范
注意大小写,注意缩进,括号一定要配对,注意要分行。
5.具体设计
系统开始,随机产生10道加法或者减法,计算完成后,统计做了多少道题,正确率,以及用了多少时间,系统结束。
6.具体编码
import java.util.InputMismatchException;
import java.util.Random;
import java.util.Scanner;
import java.io.*;
public class Pratices {
public static void main(String[] args) {
new Pratices().list_Pratices();
}
public int random_Num(int range) {
return (int) (Math.random() * range);
}
public void list_Pratices() {
int right = 0;
int wrongtimes = 0;
int num_1, num_2, temp;
int type = random_Num(4);
int score = 0;
int count = 1;
System.out.println("请输入题目数量:");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
while (count <= n) {
type = random_Num(2);
num1 = random_Num(100); //100以内随机数
num2 = random_Num(100); //100以内的随机数
wrongtimes = 0;
if (type == 0)
{
System.out.print("(" + count + ") " + num1 + " + " + num2+ " = ");//加法
}
else if(type == 1)
{
if ((num1 < num2))
{
temp = num1;
num1 = num2;
num2 = temp;
}
System.out.print("(" + count + ") " + num1 + " - " + num2+ " = ");//减法
}
else if(type == 2)
System.out.print("(" + count + ") " + num1 + " * " + num2+ " = ");//乘法
}
else if(type == 3)
{
if(num2!=0)
System.out.print("(" + count + ") " + num1 + " / " + num2+ " = ");//除法
else
System.out.println("分母为零");
}
int answer = this.getAnswer(count);
boolean flag = check(num1, num2, type, answer, count);
if (flag) {
right++;
System.out.println("回答正确");
score += this.getScore(wrongtimes);
} else {
while (wrongtimes < 2) {
wrongtimes++;
System.out.println("回答错误 " + wrongtimes + " 次");
answer = this.getAnswer(count);
flag = check(num1, num2, type, answer, count);
if (flag) {
score += this.getScore(wrongtimes);
right++;
wrongtimes = 0;
break;
}
}
if (wrongtimes == 3)
System.out.println("回答错误 ");
else
System.out.println("回答正确");
}
count++;
}
System.out.println("回答正确 : " + right);
System.out.println("回答错误: " + (10 - right));
System.out.println("获得分数: " + score);
System.out.println(getDegree(score));
}
public boolean check(int num_1, int num_2, int type, int my_Answer,
int count) {
int answer = 0;
if (type == 1) {
answer = num_1 - num_2;
} else if (type == 0) {
answer = num_1 + num_2;
}
return my_Answer == answer;
}
public int getAnswer(int count) {
int my_Answer = 0;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
my_Answer = Integer.parseInt(br.readLine());
} catch (IOException e) {
e.printStackTrace();
} catch (NumberFormatException e) {
System.out.println("输入有误");
return 0;
} finally {
if (count >= n && (br != null)) {//不会超出输入的n
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
br = null;
}
}
return my_Answer;
}
public int getScore(int wrongtimes) {
if (wrongtimes == 0) {
return 10;
} else if (wrongtimes == 1) {
return 7;
} else if (wrongtimes == 2) {
return 5;
} else
return 0;
}
public String getDegree(int score) {
if (score > 90)
return "SMART";
else if (score > 80)
return "GOOD";
else if (score > 70)
return "OK";
else if (score > 60)
return "PASS";
else
return "TRY AGAIN";
}
}