• 10.4日报


    今天完成四则运算程序,可以实现出指定题数的题目,能够统计正确率,并且能够重做错题。

    如下代码:

    import java.util.Random;
    import java.util.Scanner;
    public class Test2 {
     public static void main(String[] args) {
      // TODO 自动生成的方法存根
      Scanner scanner=new Scanner(System.in);
      System.out.println("请输入出题题数:");
      int sum = scanner.nextInt();
      //int c=scanner.nextInt();
      /*int sum = 0,c = 0;
      sum(sum);
      fanwei(c);*/
      int[] arr1=new int[sum*2];
      int[] xuhao=new int[sum];
      int[] shu1=new int[sum];
      int[] fuhao=new int[4];
      int[] shu2=new int[sum];
      randomArr(arr1);
      /*for(int i=0;i<rand.length;i++) {
       System.out.println(rand[i]);
      }*/
      
      System.out.println("是否出现乘除运算,是输入1,否输入0");
      //Scanner scanner=new Scanner(System.in);
      int x=scanner.nextInt();
      int k=0,m=0,right=0;
      
      switch(x) {
      case 0:
       //String[] operate=new String[]{"+","-"};
          Random rand=new Random();
          for(int i=0;i<arr1.length;i=i+2){
           int rand1=rand.nextInt(2);
           if(rand1==0) {
            System.out.println(k+1+".  "+arr1[i]+'+'+arr1[i+1]+"=");
           
            int jieguo=scanner.nextInt();
           
            if(arr1[i]+arr1[i+1]==jieguo) {
             System.out.println("正确!");right++;
            }
            else {
             System.out.println("错误,已记录在错题本");xuhao[m]=k+1;shu1[m]=arr1[i];fuhao[m]=0;shu2[m]=arr1[i+1];   m++;     }
           
            }
          
          else if(rand1==1) {
               System.out.println(k+1+".  "+arr1[i]+'-'+arr1[i+1]+"=");
             int jieguo=scanner.nextInt();
             if(arr1[i]-arr1[i+1]==jieguo) {
              System.out.println("正确!");right++;
             }
             else {
              System.out.println("错误,已记录在错题本");xuhao[m]=k+1;shu1[m]=arr1[i];fuhao[m]=1;shu2[m]=arr1[i+1];   m++;     }
           }
          
            ++k;}
         
          break;
         
      case 1:
         
           Random rand2=new Random();
           for(int i=0;i<arr1.length;i=i+2){
            int rand1=rand2.nextInt(4);
            if(rand1==0) {
             System.out.println(k+1+".  "+arr1[i]+'+'+arr1[i+1]+"=");
             int jieguo=scanner.nextInt();
             if(arr1[i]+arr1[i+1]==jieguo) {
              System.out.println("正确!");right++;
             }
             else {
              System.out.println("错误,已记录在错题本");xuhao[m]=k+1;shu1[m]=arr1[i];fuhao[m]=0;shu2[m]=arr1[i+1];   m++;     }
            
             }
            else if(rand1==1) {
                System.out.println(k+1+".  "+arr1[i]+'-'+arr1[i+1]+"=");
              int jieguo=scanner.nextInt();
              if(arr1[i]-arr1[i+1]==jieguo) {
               System.out.println("正确!");right++;
              }
              else {
               System.out.println("错误,已记录在错题本");xuhao[m]=k+1;shu1[m]=arr1[i];fuhao[m]=1;shu2[m]=arr1[i+1];   m++;     }
            }
            else if(rand1==2) {
                 System.out.println(k+1+".  "+arr1[i]+'*'+arr1[i+1]+"=");
               int jieguo=scanner.nextInt();
               if(arr1[i]*arr1[i+1]==jieguo) {
                System.out.println("正确!");right++;
               }
               else {
                System.out.println("错误,已记录在错题本");xuhao[m]=k+1;shu1[m]=arr1[i];fuhao[m]=2;shu2[m]=arr1[i+1];   m++;     }
            }
            else if(rand1==3) {
                 System.out.println(k+1+".  "+arr1[i]+'/'+arr1[i+1]+"=");
               int jieguo=scanner.nextInt();
               if(arr1[i]/arr1[i+1]==jieguo) {
                System.out.println("正确!");right++;
               }
               else {
                System.out.println("错误,已记录在错题本");xuhao[m]=k+1;shu1[m]=arr1[i];fuhao[m]=3;shu2[m]=arr1[i+1];   m++;     }
            }
             ++k;}
           break;
     }
      System.out.println("正确率:"+(right*100)/sum+"%");
      
      System.out.println("是否重做错题?是输入1,否输入0");
      int cout=scanner.nextInt();
      
      while(cout==1) {
       String[] operate=new String[]{"+","-","*","/"};
       
       for(int i=0;i<m;i++) {
        //System.out.println(xuhao[i]+'.');
        System.out.println(shu1[i]+operate[fuhao[i]]+shu2[i]+'=');
        int jieguo1=scanner.nextInt();
        if(fuhao[i]==0) {
         while(jieguo1!=shu1[i]+shu2[i]) {
          System.out.println("错误,请重新输入答案:");
          jieguo1=scanner.nextInt();
         }
         System.out.println("正确");
      }
        else if(fuhao[i]==1) {
         while(jieguo1!=shu1[i]-shu2[i]) {
          System.out.println("错误,请重新输入答案:");
          jieguo1=scanner.nextInt();
         }
         System.out.println("正确");
     }
        else if(fuhao[i]==2) {
         while(jieguo1!=shu1[i]*shu2[i]) {
          System.out.println("错误,请重新输入答案:");
          jieguo1=scanner.nextInt();
         }
         System.out.println("正确");
         }
        else if(fuhao[i]==3) {
         while(jieguo1!=shu1[i]/shu2[i]) {
          System.out.println("错误,请重新输入答案:");
          jieguo1=scanner.nextInt();
         }
         System.out.println("正确");
       }
      }
       System.out.println();
       System.out.println();
       System.out.println("已结束!!!");
       break;
    }
      if(cout==0) {System.out.println("已退出!!!");}
    }
     
     
        public static int[] randomArr(int[] arr) {
            Random rd = new Random();
            for (int i = 0; i < arr.length; i++) {
                int count = 0;
    //            arr[i]=rd.nextInt(10)+1;//常规来说是这样.但是我想输出不重复的随机数
                int temp = rd.nextInt(100);//先将随机数赋值给一个中间变量
                //然后再遍历一遍数组,如果有相同的,就不算,并且i--重新随机.
                for (int j = 0; j < arr.length; j++) {
                    if (arr[j] == temp) {
                        count++;//在外循环定义一个计数器,在内循环内判断是否重复.否则循环结束
                        //内循环中的变量不能用于外循环
                    }
                }if (count>0){//内循环结束,在外循环判断是否计数器>0,如果是,就i--,重新随机.
                   //否则赋值给数组
                    i--;
                }else{
                    arr[i] = temp;
                }
            }
            return arr;
        }
       
       
     
     
    }
  • 相关阅读:
    tomcat 虚拟目录
    linux 基本常用命令
    linux shell
    分布式锁
    多线程 ThreadLocal
    Java并发包中Lock的实现原理
    spring 异常处理
    spring Cache /Redis 缓存 + Spring 的集成示例
    spring 事务传播行为
    Vue.nextTick浅析
  • 原文地址:https://www.cnblogs.com/cdl-sunshine/p/13768586.html
Copyright © 2020-2023  润新知