出题类:package test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Random;
import java.util.regex.Pattern;
public class AddsubCalc {
private int num = -1; // 题数
private LinkedHashMap<String, int[]> subject; // key:题目,value[]:答案 结果
private static final char ADD = '+';
private static final char SUB = '-';
public int getNum() {
return num;
}
/**
* 出题数目
* @throws IOException
*/
public void setNum() throws IOException {
System.out.print("请输入出题数目:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = null;
while(!isNumber(line = br.readLine())) {
System.out.println("请输入数字!");
System.out.print("请输入出题数目:");
continue;
}
this.num = Integer.parseInt(line);
}
public AddsubCalc() {
super();
this.subject = new LinkedHashMap<String, int[]>();
}
// 获取一个10以内的随机数
private int getWithinten() {
return (int) (Math.random() * 10 + 1);
}
// 获取随机+ -运算符
private char getOperator() {
return new Random().nextBoolean() ? ADD : SUB;
}
// 生成题目
private void makeSubject() {
// 根据输入的数量生成表达式和结果
int a, b, result;
while (this.subject.size() < this.num) {
StringBuilder s = new StringBuilder();
a = getWithinten();
b = getWithinten();
s.append(a);
if (getOperator() == ADD) {
result = a + b;
s.append(ADD);
} else {
result = a - b;
s.append(SUB);
}
s.append(b);
s.append(" = ");
int tmp[] = { result, 0 };
this.subject.put(s.toString(), tmp);
}
}
/**
* 获取题目
*/
public void outSubject() {
if(this.num==-1) {
throw new RuntimeException("请先调用setNum设置出题数目!");
}
this.makeSubject();
for (String subject : subject.keySet()) {
System.out.println(subject);
}
}
// 判断是否为Int
private boolean isInteger(String str) {
Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
return pattern.matcher(str.trim()).matches();
}
// 判断一个字符串是否由数字组成
public boolean isNumber(String temp) {
char[] data = temp.trim().toCharArray();
for (int x = 0; x < data.length; x++) {
if (data[x] > '9' || data[x] < '0') {
return false;// 后续不再判断
}
}
return true;// 如果全部验证通过返回true
}
/**
* 答题
* @throws IOException
*/
public void getAnswer() throws IOException {
if(this.subject.size()==0) {
throw new RuntimeException("请先调用outSubject获取题目!");
}
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = null;
for (Map.Entry<String, int[]> m : this.subject.entrySet()) {
String q = m.getKey();
System.out.print(q);
while (true) {
if (!isInteger(line = br.readLine())) {
System.out.println("请输入数字!");
System.out.print(q);
continue;
}
int[] v = m.getValue();
v[1] = Integer.parseInt(line);
m.setValue(v);
break;
}
}
}
/**
* 答题结果
*/
public void getResult() {
int totalScore = 0;
for (Map.Entry<String, int[]> m : this.subject.entrySet()) {
String k = m.getKey();
int[] v = m.getValue();
String tmp;
tmp = k + Integer.toString(v[0]) + " 你 " + Integer.toString(v[1]);
if (v[0] == v[1]) {
tmp = tmp + " √";
totalScore += 1;
} else {
tmp = tmp + " ×";
}
System.out.println(tmp);
}
System.out.println("总分 " + this.subject.size() + "分 你的分数是:" + totalScore);
}
}
调用:package test;
import java.io.IOException;
public class MathProblem {
public static void main(String[] args) throws IOException {
AddsubCalc calc = new AddsubCalc();
// 出题数目
calc.setNum();
// 获取题目
calc.outSubject();
// 答题
System.out.println("请回答:");
calc.getAnswer();
// 结果
System.out.println("结果:");
calc.getResult();
}
}
|