zoukankan      html  css  js  c++  java
  • 课堂测 四则运算阶段三

    import java.util.Random;
    import java.util.Scanner;

    import javax.script.ScriptEngine;
    import javax.script.ScriptEngineManager;
    import javax.script.ScriptException;

    public class Test1to3 {

    public static double eval(String str) {

    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine se = manager.getEngineByName("js");
    Double result = null;

    try {
    result = Double.valueOf(se.eval(str).toString());
    if (result == null) {
    result = new Double(0);
    }
    } catch (ScriptException e) {
    e.printStackTrace();
    }
    return result;
    }

    public static void exercise(int grade,int num1,int num2) {//num1为出题个数,num2为操作数数值范围
    if(grade==2) {
    int n1;
    int n2;
    int i,j,rightnum=0,i1=0,i2=0,i3=0,i4=0,choice;
    char c;
    double sum,accurary;
    int number1[]=new int[100];
    int number2[]=new int[100];
    int number3[]=new int[100];
    int number4[]=new int[100];
    int number5[]=new int[100];
    int number6[]=new int[100];
    int number7[]=new int[100];
    int number8[]=new int[100];
    char[] operator=new char[]{'+','-','*','/'};
    Random rand = new Random();
    for(i=1;i<=num1;i++) {
    j=rand.nextInt(4);
    c=operator[j];
    do {
    n1=rand.nextInt(num2);
    n2=rand.nextInt(num2);
    }while(n1==0&&n2==0);
    if(c=='+') {
    int result;
    sum=n1+n2;
    System.out.print("第"+i+"道题为:");
    System.out.println(n1+"+"+n2+"=");
    System.out.println("请输入计算结果:");
    Scanner sc=new Scanner(System.in);
    result=sc.nextInt();
    System.out.println("答题结果为:"+n1+"+"+n2+"="+result);
    if(result==sum) {
    rightnum+=1;
    }
    else {
    i1+=1;
    number1[i1]=n1;
    number2[i1]=n2;
    }
    }
    else if(c=='-') {
    int result;
    sum=n1-n2;
    System.out.print("第"+i+"道题为:");
    System.out.println(n1+"-"+n2+"=");
    System.out.println("请输入计算结果:");
    Scanner sc=new Scanner(System.in);
    result=sc.nextInt();
    System.out.println("答题结果为:"+n1+"-"+n2+"="+result);
    if(result==sum) {
    rightnum+=1;
    }
    else {
    i2+=1;
    number3[i2]=n1;
    number4[i2]=n2;
    }
    }
    else if(c=='*') {
    int result;
    sum=n1*n2;
    System.out.print("第"+i+"道题为:");
    System.out.println(n1+"*"+n2+"=");
    System.out.println("请输入计算结果:");
    Scanner sc=new Scanner(System.in);
    result=sc.nextInt();
    System.out.println("答题结果为:"+n1+"*"+n2+"="+result);
    if(result==sum) {
    rightnum+=1;
    }
    else {
    i3+=1;
    number5[i3]=n1;
    number6[i3]=n2;
    }
    }
    else if(c=='/') {
    int result;
    if(n1%n2!=0) {
    do {
    n1=rand.nextInt(num2);
    n2=rand.nextInt(num2);
    }while(n1%n2!=0);
    }
    sum=(double)n1/(double)n2;
    System.out.print("第"+i+"道题为:");
    System.out.println(n1+"/"+n2+"=");
    System.out.println("请输入计算结果:");
    Scanner sc=new Scanner(System.in);
    result=sc.nextInt();
    System.out.println("答题结果为:"+n1+"/"+n2+"="+result);
    if(result==sum) {
    rightnum+=1;
    }
    else {
    i4+=1;
    number7[i4]=n1;
    number8[i4]=n2;
    }
    }
    }
    accurary=(double)rightnum/num1;
    System.out.println("您的正确率为:"+accurary*100+"%");
    System.out.println("您的错题集为:");
    for(i=1;i<=i1;i++) {
    System.out.println(number1[i]+"+"+number2[i]+"=");
    }
    for(i=1;i<=i2;i++) {
    System.out.println(number3[i]+"-"+number4[i]+"=");
    }
    for(i=1;i<=i3;i++) {
    System.out.println(number5[i]+"*"+number6[i]+"=");
    }
    for(i=1;i<=i4;i++) {
    System.out.println(number7[i]+"/"+number8[i]+"=");
    }
    do {
    System.out.println("错题重练请按1,结束练习请按0:");
    Scanner in=new Scanner(System.in);
    choice=in.nextInt();
    if(choice==1) {
    for(i=1;i<=i1;i++) {
    double result;
    int wrongnum=1;
    System.out.println(number1[i]+"+"+number2[i]+"=");
    sum=number1[i]+number2[i];
    System.out.println("请输入计算结果:");
    result=in.nextDouble();
    System.out.println("答题结果为:"+number1[i]+"+"+number2[i]+"="+result);
    if(result!=sum) {
    wrongnum+=1;
    System.out.println("回答错误,该题错误次数为"+wrongnum);
    }
    else {
    System.out.println("回答正确,本题错误次数为:"+wrongnum);
    }
    }
    for(i=1;i<=i2;i++) {
    double result;
    int wrongnum=1;
    System.out.println(number3[i]+"-"+number4[i]+"=");
    sum=number3[i]-number4[i];
    System.out.println("请输入计算结果:");
    result=in.nextDouble();
    System.out.println("答题结果为:"+number3[i]+"-"+number4[i]+"="+result);
    if(result!=sum) {
    wrongnum+=1;
    System.out.println("回答错误,该题错误次数为"+wrongnum);
    }
    else {
    System.out.println("回答正确,本题错误次数为:"+wrongnum);
    }
    }
    for(i=1;i<=i3;i++) {
    double result;
    int wrongnum=1;
    System.out.println(number5[i]+"*"+number6[i]+"=");
    sum=number5[i]*number6[i];
    System.out.println("请输入计算结果:");
    result=in.nextDouble();
    System.out.println("答题结果为:"+number5[i]+"*"+number6[i]+"="+result);
    if(result!=sum) {
    wrongnum+=1;
    System.out.println("回答错误,该题错误次数为"+wrongnum);
    }
    else {
    System.out.println("回答正确,本题错误次数为:"+wrongnum);
    }
    }
    for(i=1;i<=i4;i++) {
    double result;
    int wrongnum=1;
    System.out.println(number7[i]+"/"+number8[i]+"=");
    sum=(double)number7[i]/(double)number8[i];
    System.out.println("请输入计算结果:");
    result=in.nextDouble();
    System.out.println("答题结果为:"+number7[i]+"/"+number8[i]+"="+result);
    if(result!=sum) {
    wrongnum+=1;
    System.out.println("回答错误,该题错误次数为"+wrongnum);
    }
    else {
    System.out.println("回答正确,本题错误次数为:"+wrongnum);
    }
    }
    }
    }while(choice==1);
    }
    if(grade==3) {
    int num,n1,n2,n3,n4,i,j,k,l,wrongnum=0,choice;
    char operator1,operator2,operator3;
    char[] operator=new char[]{'+','-','*','/'};
    String[] wrong=new String[100];
    String[] couts=new String[100];
    double[] sum=new double[100];
    Scanner sc=new Scanner(System.in);
    Random rand=new Random();
    System.out.println("请输入操作数个数:");
    num=sc.nextInt();
    if(num==3) {
    for(i=1;i<=num1;i++) {
    int result;
    n1=rand.nextInt(num2);
    n2=rand.nextInt(num2);
    n3=rand.nextInt(num2);
    j=rand.nextInt(4);
    k=rand.nextInt(4);
    operator1=operator[j];
    operator2=operator[k];
    String s1 = String.valueOf(n1);//将数字n1转换为字符串s1
    String s2 = String.valueOf(n2);//将数字n2转换为字符串s2
    String s3 = String.valueOf(n3);//将数字n3转换为字符串s3
    String s4 = String.valueOf(operator1);//将字符operator1转换为字符串s4
    String s5 = String.valueOf(operator2);//将字符operator2转换为字符串s5
    String s6 ="=";
    String cout=s1+s4+s2+s5+s3+s6;
    couts[i]=s1+s4+s2+s5+s3;
    System.out.println(cout);
    sum[i]=eval(couts[i]);
    result=sc.nextInt();
    if(result!=sum[i]) {
    wrong[wrongnum]=cout;
    wrongnum+=1;
    }
    }
    System.out.println("您的错题集为:");
    for(i=0;i<wrongnum;i++) {
    System.out.println(wrong[i]);
    }
    do {
    System.out.println("错题重练请按1,退出请按0");
    choice=sc.nextInt();
    if(choice==1) {
    int result;
    for(i=0;i<wrongnum;i++) {
    int wrongn=1;//记录错误次数
    System.out.println(wrong[i]);
    result=sc.nextInt();
    if(result==sum[i+1]) {
    System.out.println("回答正确,该题错误次数为"+wrongn);
    }
    else {
    wrongn+=1;
    System.out.println("回答错误,该题错误次数为"+wrongn);
    }
    }
    }
    }while(choice==1);
    }
    if(num==4) {
    for(i=1;i<=num1;i++) {
    int result;
    n1=rand.nextInt(num2);
    n2=rand.nextInt(num2);
    n3=rand.nextInt(num2);
    n4=rand.nextInt(num2);
    j=rand.nextInt(4);
    k=rand.nextInt(4);
    l=rand.nextInt(4);
    operator1=operator[j];
    operator2=operator[k];
    operator3=operator[l];
    String s1 = String.valueOf(n1);//将数字n1转换为字符串s1
    String s2 = String.valueOf(n2);//将数字n2转换为字符串s2
    String s3 = String.valueOf(n3);//将数字n3转换为字符串s3
    String s4 = String.valueOf(n3);//将数字n4转换为字符串s4
    String s5 = String.valueOf(operator1);//将字符operator1转换为字符串s4
    String s6 = String.valueOf(operator2);//将字符operator2转换为字符串s5
    String s7 = String.valueOf(operator3);//将字符operator2转换为字符串s5
    String s8 ="=";
    String cout=s1+s5+s2+s6+s3+s7+s4+s8;
    couts[i]=s1+s5+s2+s6+s3+s7+s4;
    System.out.println(cout);
    sum[i]=eval(couts[i]);
    result=sc.nextInt();
    if(result!=sum[i]) {
    wrong[wrongnum]=cout;
    wrongnum+=1;
    }
    }
    System.out.println("您的错题集为:");
    for(i=0;i<wrongnum;i++) {
    System.out.println(wrong[i]);
    }
    do {
    System.out.println("错题重练请按1,退出请按0");
    choice=sc.nextInt();
    if(choice==1) {
    int result;
    for(i=0;i<wrongnum;i++) {
    int wrongn=1;//记录错误次数
    System.out.println(wrong[i]);
    result=sc.nextInt();
    if(result==sum[i+1]) {
    System.out.println("回答正确,该题错误次数为"+wrongn);
    }
    else {
    wrongn+=1;
    System.out.println("回答错误,该题错误次数为"+wrongn);
    }
    }
    }
    }while(choice==1);
    }
    }
    if(grade==4) {//四年级题目
    int num,n1,n2,n3,n4,n5,i,j,k,l,m,wrongnum=0,choice;
    char operator1,operator2,operator3,operator4;
    char[] operator=new char[]{'+','-','*','/'};
    String[] wrong=new String[100];
    String[] couts=new String[100];
    double[] sum=new double[100];
    Scanner sc=new Scanner(System.in);
    Random rand=new Random();
    System.out.println("请输入操作数个数:");
    num=sc.nextInt();
    if(num==3) {
    for(i=1;i<=num1;i++) {
    int result;
    n1=rand.nextInt(num2);
    n2=rand.nextInt(num2);
    n3=rand.nextInt(num2);
    j=rand.nextInt(4);
    k=rand.nextInt(4);
    operator1=operator[j];
    operator2=operator[k];
    String s1 = String.valueOf(n1);//将数字n1转换为字符串s1
    String s2 = String.valueOf(n2);//将数字n2转换为字符串s2
    String s3 = String.valueOf(n3);//将数字n3转换为字符串s3
    String s4 = String.valueOf(operator1);//将字符operator1转换为字符串s4
    String s5 = String.valueOf(operator2);//将字符operator2转换为字符串s5
    String s6 ="=";
    String cout=s1+s4+s2+s5+s3+s6;
    couts[i]=s1+s4+s2+s5+s3;
    System.out.println(cout);
    sum[i]=eval(couts[i]);
    result=sc.nextInt();
    if(result!=sum[i]) {
    wrong[wrongnum]=cout;
    wrongnum+=1;
    }
    }
    System.out.println("您的错题集为:");
    for(i=0;i<wrongnum;i++) {
    System.out.println(wrong[i]);
    }
    System.out.println("错题重练请按1,退出请按0");
    choice=sc.nextInt();
    if(choice==1) {
    int result;
    for(i=0;i<wrongnum;i++) {
    System.out.println(wrong[i]);
    result=sc.nextInt();
    if(result==sum[i+1]) {
    System.out.println("回答正确,该题错误次数为");
    }
    }
    }
    }
    if(num==4) {
    for(i=1;i<=num1;i++) {
    int result;
    n1=rand.nextInt(num2);
    n2=rand.nextInt(num2);
    n3=rand.nextInt(num2);
    n4=rand.nextInt(num2);
    j=rand.nextInt(4);
    k=rand.nextInt(4);
    l=rand.nextInt(4);
    operator1=operator[j];
    operator2=operator[k];
    operator3=operator[l];
    String s1 = String.valueOf(n1);//将数字n1转换为字符串s1
    String s2 = String.valueOf(n2);//将数字n2转换为字符串s2
    String s3 = String.valueOf(n3);//将数字n3转换为字符串s3
    String s4 = String.valueOf(n3);//将数字n4转换为字符串s4
    String s5 = String.valueOf(operator1);//将字符operator1转换为字符串s4
    String s6 = String.valueOf(operator2);//将字符operator2转换为字符串s5
    String s7 = String.valueOf(operator3);//将字符operator2转换为字符串s5
    String s8 ="=";
    String cout=s1+s5+s2+s6+s3+s7+s4+s8;
    couts[i]=s1+s5+s2+s6+s3+s7+s4;
    System.out.println(cout);
    sum[i]=eval(couts[i]);
    result=sc.nextInt();
    if(result!=sum[i]) {
    wrong[wrongnum]=cout;
    wrongnum+=1;
    }
    }
    System.out.println("您的错题集为:");
    for(i=0;i<wrongnum;i++) {
    System.out.println(wrong[i]);
    }
    do {
    System.out.println("错题重练请按1,退出请按0");
    choice=sc.nextInt();
    if(choice==1) {
    int result;
    for(i=0;i<wrongnum;i++) {
    int wrongn=1;//记录错误次数
    System.out.println(wrong[i]);
    result=sc.nextInt();
    if(result==sum[i+1]) {
    System.out.println("回答正确,该题错误次数为"+wrongn);
    }
    else {
    wrongn+=1;
    System.out.println("回答错误,该题错误次数为"+wrongn);
    }
    }
    }
    }while(choice==1);
    }
    if(num==5) {
    for(i=1;i<=num1;i++) {
    int result;
    n1=rand.nextInt(num2);
    n2=rand.nextInt(num2);
    n3=rand.nextInt(num2);
    n4=rand.nextInt(num2);
    n5=rand.nextInt(num2);
    j=rand.nextInt(4);
    k=rand.nextInt(4);
    l=rand.nextInt(4);
    m=rand.nextInt(4);
    operator1=operator[j];
    operator2=operator[k];
    operator3=operator[l];
    operator4=operator[m];
    String s1 = String.valueOf(n1);//将数字n1转换为字符串s1
    String s2 = String.valueOf(n2);//将数字n2转换为字符串s2
    String s3 = String.valueOf(n3);//将数字n3转换为字符串s3
    String s4 = String.valueOf(n3);//将数字n4转换为字符串s4
    String s5 = String.valueOf(n3);//将数字n5转换为字符串s5
    String s6 = String.valueOf(operator1);//将字符operator1转换为字符串s6
    String s7 = String.valueOf(operator2);//将字符operator2转换为字符串s7
    String s8 = String.valueOf(operator3);//将字符operator3转换为字符串s8
    String s9 = String.valueOf(operator4);//将字符operator4转换为字符串s9
    String s10 ="=";
    String cout=s1+s6+s2+s7+s3+s8+s4+s9+s5+s10;
    couts[i]=s1+s6+s2+s7+s3+s8+s4+s9+s5;
    System.out.println(cout);
    sum[i]=eval(couts[i]);
    result=sc.nextInt();
    if(result!=sum[i]) {
    wrong[wrongnum]=cout;
    wrongnum+=1;
    }
    }
    System.out.println("您的错题集为:");
    for(i=0;i<wrongnum;i++) {
    System.out.println(wrong[i]);
    }
    do {
    System.out.println("错题重练请按1,退出请按0");
    choice=sc.nextInt();
    if(choice==1) {
    int result;
    for(i=0;i<wrongnum;i++) {
    int wrongn=1;//记录错误次数
    System.out.println(wrong[i]);
    result=sc.nextInt();
    if(result==sum[i+1]) {
    System.out.println("回答正确,该题错误次数为"+wrongn);
    }
    else {
    wrongn+=1;
    System.out.println("回答错误,该题错误次数为"+wrongn);
    }
    }
    }
    }while(choice==1);
    }
    }
    }
    public static void main(String[] args) {
    // TODO 自动生成的方法存根

    int grade,num1,num2,judge;
    Scanner sc=new Scanner(System.in);
    System.out.println("请依次输入年级、出题个数、操作数范围:");
    System.out.println("(注:二年级操作数范围不超过100,三、四年级操作数范围不超过1000)");
    grade=sc.nextInt();
    num1=sc.nextInt();
    num2=sc.nextInt();
    if(grade==2&&num2>100) {
    System.out.println("操作数范围不符合规范,已退出答题!");
    System.exit(0);
    }
    else if(grade==3&&num2>1000) {
    System.out.println("操作数范围不符合规范,已退出答题!");
    System.exit(0);
    }
    else if(grade==4&&num2>1000) {
    System.out.println("操作数范围不符合规范,已退出答题!");
    System.exit(0);
    }
    else {
    exercise(grade,num1,num2);
    }
    }

    }

  • 相关阅读:
    制作计算器的代码(C#)
    Oracle如何实现创建数据库、备份数据库及数据导出导入操作
    XmlDocument类
    Android强大的开源库与系统架构工具
    IO
    胎压监测设备
    福施福、爱乐维、玛特纳各成分比较(已换算成同一单位)
    用车不容忽视的细节
    汽车必备车饰和常用物品
    j2ee指导型框架或示例
  • 原文地址:https://www.cnblogs.com/zhaoyuxiao000/p/14169015.html
Copyright © 2011-2022 走看看