四则运算(三年级)

package test;

 

import java.util.Scanner;

import java.util.Random;

public class Main {

  public static void main(String[] args)

  {

         Random n = new Random();//随机函数

         int[] a = new int[30];//存储式子序号

         int[] b = new int[30];//存储第一个数

         int[] c = new int[30];//存储运算符号对应的数字

         int[] d = new int[30];//存储第二个数

         int[] e = new int[30];//存储输入的结果

         int[] f = new int[30];//存储正确结果

         for(int i = 1;i <= 30; i++)//出题

         {

                System.out.print(i+". ");

                a[i-1] = i;

                b[i-1] = n.nextInt(100)+1;

                System.out.print(b[i-1]);

                int j = n.nextInt(4) + 1;

                c[i-1] = j;

                switch(j)//随机出符号

                {

                       case 1:System.out.print("+");break;

                       case 2:System.out.print("-");break;

                       case 3:System.out.print("*");break;

                       case 4:System.out.print("/");break;

                }

                if(j == 2) d[i-1] = n.nextInt(b[i-1])+1;//减法第二个数字不能比第一个大

                else if(j == 4) //除法结果需要为整数

                {

                       int m = 1;

                       int p = 1;

                       while(p > 0)//判断能否被整除

                       {

                              m = n.nextInt(b[i-1])+1;

                              if(b[i-1]%m == 0) p = 0;

                       }

                       d[i-1] = m;

                }

                else d[i-1] = n.nextInt(100)+1;

                System.out.print(d[i-1]);

                System.out.println("=");

                Scanner scanner = new Scanner(System.in);

                e[i-1] = scanner.nextInt();

         }

         int k = 0;

         System.out.println("正确的题目有:");

         for(int j = 0;j < 30;j++)//输出正确的题目序号

         {

                switch(c[j])

                {

                       case 1:f[j] = b[j] + d[j];break;

                       case 2:f[j] = b[j] - d[j];break;

                       case 3:f[j] = b[j] * d[j];break;

                       case 4:f[j] = b[j] / d[j];break;

                }

                if(f[j] == e[j]) //判断结果是否正确

                {

                       System.out.print(a[j]+" ");

                       k++;//计数正确题数

                }

         }

         System.out.println("共正确"+k+"道题!!!");

  }

}

posted @ 2018-10-07 17:11  zhoulonghai  阅读(327)  评论(0编辑  收藏  举报