没有使用栈来完成,不够简约,仍需改进。但是实现了继承和基本要求
2年纪类
package arith;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Random;
public class Arithmetics {
static int n1 = 30;
static int n3;
static double sumt=0;
static double sum=0;
ArrayList<Integer> Numbers = new ArrayList<>();
ArrayList<String> Symbols = new ArrayList<>();
ArrayList<Integer> Anwsers = new ArrayList<>();
static ArrayList<Integer> rNumbers = new ArrayList<>();
static ArrayList<String> rSymbols = new ArrayList<>();
static ArrayList<Integer> rAnwsers = new ArrayList<>();
//输入参数
public void getargu() {
int num1 = 30;
int num3 = 0;
Scanner sc = new Scanner(System.in);
System.out.println("请输入出题个数:");
num1 = sc.nextInt();
System.out.println("请输入操作数范围最大值:");
num3 = sc.nextInt();
n1=num1;
n3=argu(num3);
}
public int argu(int n3) {
if(n3>100) {
return 100;
}
else {
return n3;
}
}
//出题
public void draw(int nu1,int nu3) {
int s2 = 2;
Random rand = new Random();
Scanner sc = new Scanner(System.in);
int []numbers = new int[s2];
int []dsymbol = new int[s2-1];
String []symbols = new String[s2-1];
for(int i=0;i<nu1;i++) {
for(int j=0;j<s2;j++){
numbers[j] = rand.nextInt(nu3)+1;
}
for(int k=0;k<(s2-1);k++){
dsymbol[k] = rand.nextInt(4);
switch (dsymbol[k]) {
case 0:
symbols[k] = "+";
break;
case 1:
symbols[k] = "-";
break;
case 2:
symbols[k] = "*";
break;
case 3:
symbols[k] = "/";
break;
}
}
for(int e=0,t1=0,t2=0;e<s2*2;e++){
if(e%2==0){
Numbers.add(numbers[t1]);
t1++;
}
else if(e%2==1&&(e+1)%(s2*2)!=0){
Symbols.add(symbols[t2]);
if(symbols[t2]=="/") {
numbers[t2+1]=primefactor(numbers[t2]);
}
t2++;
}
else if(e%2==1&&(e+1)%(s2*2)==0) {
Symbols.add("=");
}
}
int answer = 0;
for(int e=0,t1=0,t2=0;e<s2;e++) {
if(e==0){
answer=answer+numbers[t1];
t1++;
}
else {
switch (symbols[t2]) {
case "+":
answer=answer+numbers[t1];
break;
case "-":
answer=answer-numbers[t1];
break;
case "*":
answer=answer*numbers[t1];
break;
case "/":
answer=answer/numbers[t1];
break;
}
t2++;
}
}
Anwsers.add(answer);
}
}
//判断正误
public boolean judge(int myanswer,int l){
if(Anwsers.get(l)==myanswer) {
return true;
}
else {
return false;
}
}
//做题
public void problem() {
Scanner sc = new Scanner(System.in);
int answer = 0;
int s2=2;
int s =0;
while(true) {
if(Symbols.get(s)=="=") {
s2=(s+1);
break;
}else {
s++;
}
}
for(int i=0;i<n1;i++) {
for(int j=0,t1=0,t2=0;j<100;j++) {
if(j%2==0) {
System.out.print(Numbers.get((i*s2)+t1));
t1++;
}
else if(j%2==1&&Symbols.get(t2)!="=") {
System.out.print(Symbols.get((i*s2)+t2));
t2++;
}
else if(j%2==1&&Symbols.get(t2)=="=") {
System.out.println(Symbols.get((i*s2)+t2));
t2++;
break;
}
}
answer = sc.nextInt();
if(judge(answer,i)) {
System.out.println("正确");
sumt++;
sum++;
}
else {
System.out.println("错误");
for(int e=0,t1=0,t2=0;e<s2*2;e++){
if(e%2==0){
rNumbers.add(Numbers.get((i*s2)+t1));
t1++;
}
else if(e%2==1){
rSymbols.add(Symbols.get((i*s2)+t2));
t2++;
}
}
rAnwsers.add(Anwsers.get(i));
sum++;
}
}
}
//错题集
public void wrongproblem() {
System.out.println("复习错题输入1");
System.out.println("查询正确率输入2");
Scanner sc = new Scanner(System.in);
int x1 = sc.nextInt();
if(x1==1) {
remake();
}else {
double rate;
rate = sumt/sum;
System.out.println(rate);
}
}
//重做
public void remake() {
Scanner sc = new Scanner(System.in);
int answer = 0;
int s2=2;
int s =0;
while(true) {
if(rSymbols.get(s)=="=") {
s2=(s+1);
break;
}else {
s++;
}
}
for(int i=0;i<n1;i++) {
for(int j=0,t1=0,t2=0;j<100;j++) {
if(j%2==0) {
System.out.print(rNumbers.get((i*s2)+t1));
t1++;
}
else if(j%2==1&&rSymbols.get(t2)!="=") {
System.out.print(rSymbols.get((i*s2)+t2));
t2++;
}
else if(j%2==1&&rSymbols.get(t2)=="=") {
System.out.println(rSymbols.get((i*s2)+t2));
t2++;
break;
}
}
answer = sc.nextInt();
if(judge(answer,i)) {
System.out.println("正确");
}
else {
System.out.println("错误");
}
}
}
//解决必须整除问题
public int primefactor(int n){
int m = 0;
if(n<1){
m= -1;
}
else if(n==1){
m= 1;
}
else {
while(n>1){
for(int i=2;i<=n;i++){
if (n==i){
m=n;
}
if(n%i==0){
n=n/i;
break;
}
}
}
}
return m;
}
}
3年级类,继承2年级
package arith;
import java.util.Random;
import java.util.Scanner;
public class Arithmetics3 extends Arithmetics{
public int argu(int n3) {
if(n3>1000) {
return 1000;
}
else {
return n3;
}
}
public void draw(int nu1,int nu3) {
int s2 = 4;
Random rand = new Random();
Scanner sc = new Scanner(System.in);
int []numbers = new int[s2];
int []dsymbol = new int[s2-1];
String []symbols = new String[s2-1];
for(int i=0;i<nu1;i++) {
for(int j=0;j<s2;j++){
numbers[j] = rand.nextInt(nu3)+1;
}
for(int k=0;k<(s2-1);k++){
dsymbol[k] = rand.nextInt(4);
switch (dsymbol[k]) {
case 0:
symbols[k] = "+";
break;
case 1:
symbols[k] = "-";
break;
case 2:
symbols[k] = "*";
break;
case 3:
symbols[k] = "/";
break;
}
}
for(int e=0,t1=0,t2=0;e<s2*2;e++){
if(e%2==0){
Numbers.add(numbers[t1]);
t1++;
}
else if(e%2==1&&(e+1)%(s2*2)!=0){
Symbols.add(symbols[t2]);
if(symbols[t2]=="/") {
numbers[t2+1]=primefactor(numbers[t2]);
}
t2++;
}
else if(e%2==1&&(e+1)%(s2*2)==0) {
Symbols.add("=");
}
}
int answer = 0;
for(int e=0,t1=0,t2=0;e<s2;e++) {
if(e==0){
answer=answer+numbers[t1];
t1++;
}
else {
switch (symbols[t2]) {
case "+":
answer=answer+numbers[t1];
break;
case "-":
answer=answer-numbers[t1];
break;
case "*":
answer=answer*numbers[t1];
break;
case "/":
answer=answer/numbers[t1];
break;
}
t2++;
}
}
Anwsers.add(answer);
}
}
}
4年级类,继承3年级
package arith;
import java.util.Random;
import java.util.Scanner;
public class Arithmetics4 extends Arithmetics3{
public int argu(int n3) {
return n3;
}
public void draw(int nu1,int nu3) {
int s2 = 5;
Random rand = new Random();
Scanner sc = new Scanner(System.in);
int []numbers = new int[s2];
int []dsymbol = new int[s2-1];
String []symbols = new String[s2-1];
for(int i=0;i<nu1;i++) {
for(int j=0;j<s2;j++){
numbers[j] = rand.nextInt(nu3)+1;
}
for(int k=0;k<(s2-1);k++){
dsymbol[k] = rand.nextInt(4);
switch (dsymbol[k]) {
case 0:
symbols[k] = "+";
break;
case 1:
symbols[k] = "-";
break;
case 2:
symbols[k] = "*";
break;
case 3:
symbols[k] = "/";
break;
}
}
for(int e=0,t1=0,t2=0;e<s2*2;e++){
if(e%2==0){
Numbers.add(numbers[t1]);
t1++;
}
else if(e%2==1&&(e+1)%(s2*2)!=0){
Symbols.add(symbols[t2]);
if(symbols[t2]=="/") {
numbers[t2+1]=primefactor(numbers[t2]);
}
t2++;
}
else if(e%2==1&&(e+1)%(s2*2)==0) {
Symbols.add("=");
}
}
int answer = 0;
for(int e=0,t1=0,t2=0;e<s2;e++) {
if(e==0){
answer=answer+numbers[t1];
t1++;
}
else {
switch (symbols[t2]) {
case "+":
answer=answer+numbers[t1];
break;
case "-":
answer=answer-numbers[t1];
break;
case "*":
answer=answer*numbers[t1];
break;
case "/":
answer=answer/numbers[t1];
break;
}
t2++;
}
}
Anwsers.add(answer);
}
}
}
主函数
package arith;
import java.util.Scanner;
public class Test1 {
public static void main(String[] args) {
Arithmetics ari2 = new Arithmetics();
Arithmetics ari3 = new Arithmetics3();
Arithmetics ari4 = new Arithmetics4();
for(int i=0;i!=1;) {
System.out.println("**************");
System.out.println(" 0. 终止 ");
System.out.println(" 1. 参数设置 ");
System.out.println("**************");
System.out.println(" 2.小学二年级口算题");
System.out.println(" 3.小学三年级口算题");
System.out.println(" 4.小学四年级口算题");
System.out.println(" 5. 错题集 ");
System.out.println("**************");
System.out.println("**输入1-5来选择**");
Scanner sc = new Scanner(System.in);
int a1 = sc.nextInt();
switch (a1) {
case 0:
i=1;
break;
case 1:
ari2.getargu();
break;
case 2:
ari2.draw(Arithmetics.n1,Arithmetics.n3);
ari2.problem();
break;
case 3:
ari3.draw(Arithmetics.n1,Arithmetics.n3);
ari3.problem();
break;
case 4:
ari4.draw(Arithmetics.n1,Arithmetics.n3);
ari4.problem();
break;
case 5:
ari2.wrongproblem();
break;
}
}
}
}
标签:9.30,int,t2,System,t1,break,测试,answer,课堂 From: https://www.cnblogs.com/rsy-bxf150/p/16757231.html