【公基】口算提速,java 自动生成:加、减、乘、除、混合运算题目

效果图

  • 先确定训练那种运算
  • 以及训练多少题:

  • 具体运算:

  • 结果分析

代码

  • 废话不多说,直接上代码
package 行测;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.Scanner;

public class CalculateByMain {
	static enum CalculateType {
		Add,
		Subtract,
		Multiply,
		Divide,
		Mix
	}
	
	public static CalculateType chooseType() {
        System.out.println("请输入出题类型(仅输入数字)\n1:加法\n2:减法\n3、乘法\n4:除法\n5:随机");
        Scanner input = new Scanner(System.in);
        int userResult = input.nextInt();
        switch(userResult) {
        case 1:
        	return CalculateType.Add;
        case 2:
        	return CalculateType.Subtract;
        case 3:
        	return CalculateType.Multiply;
        case 4:
        	return CalculateType.Divide;
        case 5:
        	return CalculateType.Mix;
        }
        System.out.println("类型错误,请重试:");
        return chooseType();
	}
    
    public static int attachSymbolByType(CalculateType type) {
    	int symbol = 0;
    	if(type == CalculateType.Add) {
    		return 0;
    	} else if(type == CalculateType.Subtract) {
    		return 1;
    	} else if(type == CalculateType.Multiply) {
    		return 2;
    	} else if(type == CalculateType.Divide) {
    		return 3;
    	} else if(type == CalculateType.Mix) {
            Random r = new Random();
            return r.nextInt(3); // 生成[0,10]区间的整数
    	}
    	return 0;
    }
    
    public static Boolean doCalculate(CalculateType type) {
    	Boolean isRight = false;
        Random r = new Random();
        int figuresFirst = r.nextInt(899) + 100; // 生成[0,10]区间的整数
        int figuresSecond = r.nextInt(899) + 100; // 生成[0,10]区间的整数
        int symbol = attachSymbolByType(type);
        
        String formula = "";
        formula+=figuresFirst;
        double formulaResult = 0;

        if (symbol == 0) {
            formula += " + ";
            formulaResult = figuresFirst + figuresSecond;
        }else if (symbol == 1) {
            formula += " - ";
            formulaResult = figuresFirst - figuresSecond;
        }else if (symbol == 2) {
            formula += " * ";
            formulaResult = figuresFirst * figuresSecond;
        }else if (symbol == 3) {
            formula += " / ";
            formulaResult = (float) figuresFirst / figuresSecond * 1.0;
        }
    	
        formula += figuresSecond;
        formula += " = ";

        System.out.println(formula);
        Scanner input = new Scanner(System.in);
        double userResult = input.nextDouble();

    	NumberFormat nf = NumberFormat.getNumberInstance();
    	nf.setMaximumFractionDigits(3);
        isRight = Math.abs(formulaResult-userResult) < 0.1;
        System.out.print(isRight ? "\033[1;96m" + "√ 正确 罒ω罒 " +"\033[m" : "\033[1;91m" + "X 错误 (;′⌒`) "+"\033[m");
        System.out.println("答案是:" + nf.format(formulaResult));
        
        return isRight;
    }
    
    public static int scanQuestionNum() {
        System.out.println("你要刷多少题?请输入:");
        Scanner input = new Scanner(System.in);
        return input.nextInt();
    }

    public static void main(String[] args) {
    	CalculateType type = chooseType();
    	int questionNum= scanQuestionNum();
    	int rightNum = 0;
    	
    	int totalTime = 0;
    	for(int i = 0; i < questionNum; i++) {
            System.out.println("第"+(i+1)+"题,"+"还剩"+(questionNum-i-1)+"题:");
        	long frontTime = System.currentTimeMillis();
    		if(doCalculate(type)) {rightNum++;}
        	long behindTime = System.currentTimeMillis();
        	long usageTime = (behindTime - frontTime)/1000;
        	totalTime+=usageTime;
            System.out.println("用时:"+usageTime+"秒");
    	}
    	NumberFormat nf = NumberFormat.getNumberInstance();
    	nf.setMaximumFractionDigits(2);
        System.out.println("恭喜完成!正确率:" + nf.format((float)rightNum/questionNum*100) + "%");
        System.out.println("总用时:"+totalTime+"秒,平均用时"+totalTime/questionNum+"秒");
    }

}
  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值