首页 > 代码库 > 四则运算

四则运算

import java.util.Random;
 import java.util.Scanner;
 
  public class qq {
      public static void main(String[] args) {
          int e = 0, r = 0;
          System.out.println("欢迎来到四则运算测试软件!");
          System.out.println("做题前请认真读题,祝您考试愉快!");
          Random r1 = new Random();
         for (int i = 0; i < 7; i++) {
             int m, n;
             int a, b, c;
             m = r1.nextInt() % 10;
             n = m % 4;
 
             switch (n) {
             case 0:
                 a = new Random().nextInt((int) Math.pow(10, 2)) + 1;
                 b = new Random().nextInt((int) Math.pow(10, 2)) + 1;
                 System.out.println(a + "+" + "" + b + "=?");
                 Scanner x1 = new Scanner(System.in);
                 c = x1.nextInt();
                 if (c != a + b) {
                     e++;
                     System.out.println("回答错误!!");
                 } else {
                     r++;
                     System.out.println("回答正确!!");
                 }
 
                 break;
 
             case 1:
                 a = new Random().nextInt((int) Math.pow(10, 2)) + 1;
                 b = new Random().nextInt((int) Math.pow(10, 2)) + 1;
                 System.out.println(a + "-" + " " + b + " =?");
                 Scanner x2 = new Scanner(System.in);
                 c = x2.nextInt();
                 if (c != a - b) {
                     e++;
                     System.out.println("回答错误!!");
                 } else {
                     r++;
                     System.out.println("回答正确!!");
                 }
 
                 break;
 
             case 2:
                 a = new Random().nextInt((int) Math.pow(10, 2)) + 1;
                 b = new Random().nextInt((int) Math.pow(10, 2)) + 1;
                 System.out.println(a + "*" + " " + b + " " + "=?");
                 Scanner y1 = new Scanner(System.in);
                 c = y1.nextInt();
                 if (c != a * b) {
                     e++;
                     System.out.println("回答错误!!");
                 } else {
                     r++;
                     System.out.println("回答正确!!");
                 }
 
                 break;
 
             case 3:
                 double d;
                 a = new Random().nextInt((int) Math.pow(10, 2)) + 1;
                 b = new Random().nextInt((int) Math.pow(10, 2)) + 1;
                 if (b == 0)
                     b++;
                 System.out.println(a + "/" + " " + b + " " + "=?");
                 Scanner y2 = new Scanner(System.in);
                 d = y2.nextDouble();
                 if (d != (a / b) / 1.00) {
                     e++;
                     System.out.println("回答错误!!");
                 } else {
                     r++;
                     System.out.println("回答正确!!");
 
                 }

                 break;
 
             }
         }
         System.out.println("考试结束!!");
         System.out.println("您一共做了:" + (r + e) + "道题目,其中正确的有" + r + "道,错误的有" + e + "道!");
     }
 }

四则运算