在课上练习的基础上,实现输出加减法混合的运算题目列表。
package JavaTest;
import java.util.*;
import javax.script.*;
public class test1 {
static char[] fu = {'+', '-', '*', '/'}; // 符号
static String[] ti = new String[100]; // 题
static String[] daan = new String[100]; // 答案
static int tishu = 10; // 题数
static int caozuoshu = 2; // 操作数
static int fanwei = 100; // 范围
public static void choose() {
Scanner s = new Scanner(System.in);
System.out.println("**********************");
System.out.println("* 口算题生成与答题 *");
System.out.println("**********************");
System.out.println("请选择操作:");
System.out.println("1. 参数设置");
System.out.println("2. 开始出题并答题");
System.out.println("0. 退出");
int x = s.nextInt();
while (x != 0) {
switch (x) {
case 1:
canshu();
break;
case 2:
chuti(tishu, caozuoshu, fanwei);
suandaan();
zuoti();
break;
default:
System.out.println("请输入 1-2 或 0 退出");
break;
}
System.out.println("**********************");
System.out.println("* 口算题生成与答题 *");
System.out.println("**********************");
System.out.println("请选择操作:");
System.out.println("1. 参数设置");
System.out.println("2. 开始出题并答题");
System.out.println("0. 退出");
x = s.nextInt();
}
}
// 参数设置
public static void canshu() {
Scanner c = new Scanner(System.in);
System.out.println("**********************");
System.out.println("* 参数设置 *");
System.out.println("**********************");
System.out.print("请输入题数:");
tishu = c.nextInt();
System.out.print("请输入操作数:");
caozuoshu = c.nextInt();
System.out.print("请输入范围:");
fanwei = c.nextInt();
System.out.println("参数设置完成");
}
// 出题
public static void chuti(int tishu, int caozuoshu, int fanwei) {
int a = 0;
int c = 0;
int d = 0;
int e = 0;
Random r = new Random();
for (int i = 0; i < tishu; i++) {
StringBuilder b = new StringBuilder();
if (caozuoshu == 2) {
d = r.nextInt(4);
if (d == 3) {
while (true) {
e = r.nextInt(fanwei + 1);
c = r.nextInt(fanwei + 1);
a = c * e;
if (a <= fanwei && c != 0)
break;
}
} else {
a = r.nextInt(fanwei + 1);
d = r.nextInt(3);
c = r.nextInt(fanwei + 1);
}
b.append(a);
b.append(' ').append(fu[d]).append(' ');
b.append(c);
} else {
for (int j = 1; j <= 2 * caozuoshu - 1; j += 2) {
a = r.nextInt(fanwei + 1);
b.append(a);
if (j != 2 * caozuoshu - 1) {
b.append(' ').append(fu[r.nextInt(3)]).append(' ');
}
}
}
ti[i] = b.toString();
}
}
// 查重
public static boolean same() {
for (int i = 0; i < tishu; i++) {
for (int j = i + 1; j < tishu; j++) {
if (ti[i].equals(ti[j])) {
return true;
}
}
}
return false;
}
// 算答案
public static void suandaan() {
ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
ScriptEngine scriptEngine = scriptEngineManager.getEngineByName("nashorn");
for (int i = 0; i < tishu; i++) {
try {
String result = String.valueOf(scriptEngine.eval(ti[i]));
daan[i] = result;
} catch (ScriptException e) {
e.printStackTrace();
}
}
}
// 做题
public static void zuoti() {
Scanner indaan = new Scanner(System.in);
String an = "";
System.out.println("**********************");
System.out.println("* 开始答题 *");
System.out.println("**********************");
int count = 0;
for (int i = 0; i < tishu; i++) {
if (!same()) {
count++;
System.out.print(count + ".\t" + ti[i] + " = ");
an = indaan.next();
if (an.equals(daan[i])) {
System.out.println("√");
} else {
System.out.println("×");
}
} else {
i--;
}
}
System.out.println("**********************");
System.out.println("* 答题完成 *");
System.out.println("**********************");
System.out.println("正确率:" + (double) (tishu - count) * 100 / tishu + "%");
}
public static void main(String[] args) {
choose();
}
}
![]()