四则运算代码
问题:没有更好的分模块,导致主函数很长,如果再加一些条件估计就得再拆了,可读性还不是很好。
在输出函数上有重复使用的现象,会导致需要功能不一样的操作调用同一个函数,造成了冲突。
这是计算字符串的结果的函数 temp[i] = Double.valueOf(jsp.eval(a[i]).toString());
package jjcc;
import java.util.Random;
import java.util.Scanner;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class jjcc {
Scanner scan = new Scanner(System.in);
static ScriptEngine jsp = new ScriptEngineManager().getEngineByName("JavaScript");
// ************查重 出题参数 随机数字
public String suan(int nums[], int judge, int f) {// 生成一道题目 (随机数,判断乘除,判断年级)
String symbol = null;// 加减乘除符号
String num = "";// 字符数组,存放题目
int bracket1 = new Random().nextInt(nums.length - 1);// 括号位置
int bracket2 = new Random().nextInt(nums.length - 1) + bracket1 + 1;
int h = 1;
for (int i = 0; i < nums.length; i++) {
// 四年级加括号(预备)
if (bracket1 != 0 || bracket2 != nums.length - 1) {
if (f == 4 && nums.length != 2) {
if (i == bracket1) {
num = num + "(";
}
}
}
num = num + nums[i];
if (bracket1 != 0 && bracket2 != nums.length - 1) {
if (f == 4 && nums.length != 2) {
if (i == bracket2) {
num = num + ")";
h = 0;
}
}
}
int chance;
if (judge == 1) {
chance = new Random().nextInt(4) + 1;
} else {
chance = new Random().nextInt(2) + 1;
}
// 二年级判断是否能除尽
for (;;) {
if (f == 1 && chance == 4) {
if (nums[0] % nums[1] != 0) {
nums = randommath(2);
}
}
break;
}
switch (chance) {
case 1:
symbol = "+";
break;
case 2:
symbol = "-";
break;
case 3:
symbol = "*";
break;
case 4:
symbol = "/";
break;
}
if (i != nums.length - 1) {
num = num + symbol;
}
}
if (h == 1 && bracket1 != 0) {
num = num + ")";
}
return num;
}
// 随机数组
public int[] randommath(int chance2) {
int nums[] = new int[chance2];
for (int j = 0; j < nums.length; j++) {// 操作数
nums[j] = new Random().nextInt(100) + 1;// 初始化算式中的数字
}
return nums;
}
// 出题参数
public int getchengchu() {// 是否乘除
System.out.println("请输入是否有乘除法(0否1是)");
int s;// 判断乘除
s = scan.nextInt();
return s;
}
public int caozuoshu(int f) {// 操作数数量
int s;
// 二年级
if (f == 2) {
s = 2;
return s;
}
for (;;) {
System.out.println("请输入操作数数目");
s = scan.nextInt();
// 三年级
if (f == 3) {
if (s > 1 && s < 5) {
break;
} else {
}
}
// 四年级
if (f == 4) {
if (s > 1 && s < 6) {
break;
} else {
System.out.println("输入错误,请重新输入");
}
}
}
return s;
}
public int timushu() {// 题目数量
System.out.println("请输入题目数目");
int s = 0;
s = scan.nextInt();
return s;
}
public boolean next() {
System.out.println("是否进行下一套答题(0否1是)");
int next = 0;
next = scan.nextInt();
if (next == 1)
return true;
else
return false;
}
public int gradenum() {// 年级
int s;
for (;;) {
System.out.println("请输入需要生成几年级的题目(2,3,4年级)");
s = scan.nextInt();
if (s < 5 && s > 1) {
break;
} else {
System.out.println("输入错误,请重新输入");
}
}
return s;
}
// 所有题目
public String[] allsuan(int j1, int j2, int j3, int f) {
boolean judge;
String[] temp = new String[j3];
for (int i = 0; i < j3; i++) {
judge = true;
while (judge) {
int random[] = randommath(j2);
String a = suan(random, j1, f);
if (chachong(temp, a)) {// 不重复,题目可用
temp[i] = a;
judge = false;
}
}
}
return temp;
}
// 查重
public boolean chachong(String[] a, String b) {
for (int i = 0; i < a.length; i++) {
if (b.equals(a[i])) {
return false;
}
}
return true;
}
// 总输出,返回答题结果数组
public int[] display(String[] a, int j3, int c[]) {
// Thread t = new Thread(new Runnable() {
//
// @Override
// public void run() {
// // TODO Auto-generated method stub
// try {
// for (int i = 0; i < 3 && b; i++) {
// Thread.sleep(1000);
// System.out.println("Thread1()" + "Running");
// }
// } catch (Exception e) {
// // TODO: handle exception
// }
// }
// });
int answer;
int answers[] = new int[j3];
for (int i = 0; i < a.length; i++) {
if (c[i] != 0) {
System.out.println((i + 1) + ".: " + a[i] + "=" + " (已错" + (c[i]) + "次)");
} else {
System.out.println((i + 1) + ".: " + a[i] + "=");
}
System.out.print("得数:");
answer = scan.nextInt();
answers[i] = answer;// 得数存储
System.out.println();
}
return answers;
}
// 专门输出错题的
// public int[] display_2(String[] a, int j3, int c[],int total) {
// int answer;
// int answers[] = new int[j3];
// System.out.println("第" + total + "套题的错题");
// for (int i = 0; i < a.length; i++) {
// if (c[i] != 0) {
// System.out.println((i + 1) + ".: " + a[i] + "=" + " (已错" + (c[i]) + "次)");
// } else {
// System.out.println((i + 1) + ".: " + a[i] + "=");
// }
//
// System.out.print("得数:");
// answer = scan.nextInt();
// answers[i] = answer;// 得数存储
// System.out.println();
// }
// return answers;
// }
// 结果
public int[] Answer(String a[], int j3) throws NumberFormatException, ScriptException {
double temp[] = new double[j3];
int temp1[] = new int[j3];
for (int i = 0; i < j3; i++) {
temp[i] = Double.valueOf(jsp.eval(a[i]).toString());
temp1[i] = (int) temp[i];
}
return temp1;
}
// 判断得数对错 a为正确 b为作答 返回正确率
public double Judge(int a[], int b[], int j3, int c[]) {
double all = 0;
int right = 0;
double accuracy = 0;
for (int i = 0; i < j3; i++) {
all++;
if (a[i] == b[i]) {
c[i] = 0;
right++;
} else {
c[i]++;
}
}
accuracy = right / all;
return accuracy;
}
//错题
public String[] Mistakes(String str[], int a[], int b[], int j3) {
int total = 0;
String c[] = new String[j3];
for (int i = 0; i < j3; i++) {
if (a[i] != b[i]) {
c[total] = str[i];
total++;
}
}
if (total == 0) {
return null;
} else {
String d[] = new String[total];
for (int i = 0; i < total; i++) {
d[i] = c[i];
}
return d;
}
}
//判断错题
public boolean putMistakes(String str[]) {
if (str == null) {
System.out.println("宁可太厉害了嗷,没错题嗷");
return true;
} else {
return false;
}
}
//输出错题
public void putMistakes_2(String str[]) {
System.out.println("错题集:");
for (int i = 0; i < str.length; i++) {
System.out.println(str[i]);
}
}
public int[] mistakenumber(int j3) {
int temp[] = new int[j3];
for (int i = 0; i < j3; i++) {
temp[i] = 0;
}
return temp;
}
public int[] mistakenumber2(int j3s, int temp[]) {// j3s是新长度 temp是原错题次数集
int b[] = new int[j3s];
int total = 0;
for (int i = 0; i < temp.length; i++) {
if (temp[i] != 0) {
b[total] = temp[i];
total++;
}
}
return b;
}
public boolean ismistake(int a) {
if (a == 1) {
return true;
}
return false;
}
public String[] addString(String a[], String b[]) {// 数组相加
int l = a.length + b.length;
String c[] = new String[l];
for (int i = 0; i < a.length; i++) {
c[i] = a[i];
}
for (int i = a.length; i < l; i++) {
c[i] = b[i - a.length];
}
return c;
}
public int[] addint(int a[], int b[]) {// 数组相加
int l = a.length + b.length;
int c[] = new int[l];
for (int i = 0; i < a.length; i++) {
c[i] = a[i];
}
for (int i = a.length; i < l; i++) {
c[i] = b[i - a.length];
}
return c;
}
public static void main(String[] args) throws NumberFormatException, ScriptException {
Scanner scan = new Scanner(System.in);
jjcc s = new jjcc();
int total = 1;// 记录做题套数
// int num=1;
// int mistakesnum[] = new int[num];
int a, b, c, f;
f = 0;
a = 0;
b = 0;
c = 0;
f = s.gradenum();
a = s.getchengchu();
b = s.caozuoshu(f);
c = s.timushu();
int time = 100;
int misnum[] = new int[c];// 错题次数记录
misnum = s.mistakenumber(c);// 错题次数 数组
String e[];
e = s.allsuan(a, b, c, f);// 所有算式
int[] d = s.display(e, c, misnum);// 输出并返回答案
System.out.println("正确率为" + (s.Judge(s.Answer(e, c), d, c, misnum)) * 100 + "%");// 第一次做
String mistake[] = s.Mistakes(e, s.Answer(e, c), d, c);// 错题集
boolean judgemis = true;
judgemis = s.next();
while (judgemis) {// 是否进行下一套题
total++;
f = s.gradenum();
a = s.getchengchu();
b = s.caozuoshu(f);
c = s.timushu();
int misnum1[] = new int[c];// 错题次数记录
misnum1 = s.mistakenumber(c);// 错题次数 数组
e = s.allsuan(a, b, c, f);// 所有算式
d = s.display(e, c, misnum1);// 输出并返回答案
System.out.println("正确率为" + (s.Judge(s.Answer(e, c), d, c, misnum1)) * 100 + "%");
String mistake2[] = s.Mistakes(e, s.Answer(e, c), d, c);// 错题集
misnum = s.addint(misnum, misnum1);
if (mistake != null && mistake2 != null)
mistake = s.addString(mistake, mistake2);
else if (mistake != null && mistake2 == null)
mistake = mistake;
else if (mistake == null && mistake2 != null)
mistake = mistake2;
else
mistake = null;
judgemis = s.next();
} // 做错题
boolean judge;
boolean judge2 = true;
while (judge2) {
judge = s.putMistakes(mistake);
if (judge) {
judge2 = false;// 没有错题的输出方式
return;
} else {
// 有错题
e = mistake;// 第一个错题集
c = mistake.length;// 第一个错题集长度
misnum = s.mistakenumber2(c, misnum);
// System.out.println(c);
d = s.display(mistake, c, misnum);// 第一个错题集的答案
System.out.println("正确率为" + (s.Judge(s.Answer(e, c), d, c, misnum)) * 100 + "%");
///////////////
mistake = s.Mistakes(e, s.Answer(e, c), d, c);
judge2 = true;
}
}
}
}

浙公网安备 33010602011771号