小学五年级口算题(代码生成)

import java.math.BigDecimal;

public class FiveMath {
	//乘法
	public static void createMultiplication() {
        int one;
        String two, three;

        one = (int)(Math.random() * 10);
        if (one < 6) {	//小数乘小数
        	int num = (int)(Math.random() * 10);
        	if(num < 5) {	//一位小数乘一位小数
        		if((int)(Math.random() * 10) < 5) {
        			two = String.format("%.1f", Math.random() * 100);
        			three = String.format("%.1f", Math.random() * 10);
        		} else {
        			two = String.format("%.1f", Math.random() * 10);
        			three = String.format("%.1f", Math.random() * 10);
        		}
        	} else if(num < 8) {	//两位小数乘一位小数
            	two = String.format("%.2f", Math.random() * 10);
    			three = String.format("%.1f", Math.random() * 10);
        	} else {	//一位小数乘两位小数
        		two = String.format("%.1f", Math.random() * 10);
    			three = String.format("%.2f", Math.random() * 10);
        	}
        } else if (one < 8) {	//小数乘整数
        	if((int)(Math.random() * 10) < 5) {	//一位小数乘两位整数
        		two = String.format("%.1f", Math.random() * 10);
        		three = String.valueOf((int)(Math.random() * 99) + 1);
        	} else {	//两位小数乘一位整数
        		two = String.format("%.2f", Math.random() * 10);
        		three = String.valueOf((int)(Math.random() * 9) + 1);
        	}
        } else {	//整数乘小数
        	if((int)(Math.random() * 10) < 5) {	//两位整数乘一位小数
        		two = String.valueOf((int)(Math.random() * 99) + 1);        		
    			three = String.format("%.1f", Math.random() * 10);
        	} else {	//一位整数乘两位小数
        		two = String.valueOf((int)(Math.random() * 9) + 1);
    			three = String.format("%.2f", Math.random() * 10);
        	}
        }
        
        System.out.println(two + "x" + three + "=");
    }

    //除法
	public static void createDivision() {
		int one;
		String two, three;
		
        one = (int)(Math.random() * 10);
        if (one < 6) {	//小数除小数
        	two = String.format("%.1f", Math.random() * 10);
			three = String.format("%.1f", Math.random() * 10);
			
			BigDecimal p1 = new BigDecimal(two);
	        BigDecimal p2 = new BigDecimal(three);
			System.out.println(p1.multiply(p2).doubleValue() + "÷" + three + "=");
        } else if (one < 8) {	//小数除整数
        	if((int)(Math.random() * 10) < 5) {
        		two = String.format("%.1f", Math.random() * 10);
        		three = String.valueOf((int)(Math.random() * 99) + 1);
        	} else {
        		two = String.format("%.2f", Math.random() * 10);
        		three = String.valueOf((int)(Math.random() * 9) + 1);
        	}

        	BigDecimal p1 = new BigDecimal(two);
	        BigDecimal p2 = new BigDecimal(three);
			System.out.println(p1.multiply(p2).doubleValue() + "÷" + three + "=");
        } else {	//整数除小数
        	if((int)(Math.random() * 10) < 5) {
        		two = String.valueOf((int)(Math.random() * 99) + 1);
        		three = String.format("%.1f", Math.random() * 10);
        	} else {
        		two = String.valueOf((int)(Math.random() * 9) + 1);
        		three = String.format("%.2f", Math.random() * 10);
        	}

        	BigDecimal p1 = new BigDecimal(two);
	        BigDecimal p2 = new BigDecimal(three);
			System.out.println(p1.multiply(p2).doubleValue() + "÷" + three + "=");
        }
    }
	
	//加法
    public static void createAddition() {
    	int one;
        String two, three;
        
        one = (int)(Math.random() * 10);
        if (one < 6) {	//小数加小数
        	int num = (int)(Math.random() * 10);
        	if(num < 5) {	//一位小数加一位小数
        		if((int)(Math.random() * 10) < 5) {
        			two = String.format("%.2f", Math.random() * 100);
                    three = String.format("%.1f", Math.random() * 10);
        		} else {
        			two = String.format("%.1f", Math.random() * 10);
        			three = String.format("%.1f", Math.random() * 10);
        		}
        	} else if(num < 8) {	//两位小数加一位小数
        		two = String.format("%.2f", Math.random() * 10);
            	three = String.format("%.1f", Math.random() * 10);
        	} else {	//一位小数加两位小数
        		two = String.format("%.1f", Math.random() * 10);
            	three = String.format("%.2f", Math.random() * 10);
        	}
        } else if (one < 8) {	//小数加整数
        	if((int)(Math.random() * 10) < 5) {
        		two = String.format("%.1f", Math.random() * 10);
        		three = String.valueOf((int)(Math.random() * 99) + 1);
        	} else {
        		two = String.format("%.2f", Math.random() * 10);
        		three = String.valueOf((int)(Math.random() * 9) + 1);
        	}
        } else {	//整数加小数
        	if((int)(Math.random() * 10) < 5) {
        		two = String.valueOf((int)(Math.random() * 99) + 1);
        		three = String.format("%.1f", Math.random() * 10);
        	} else {
        		two = String.valueOf((int)(Math.random() * 9) + 1);
        		three = String.format("%.2f", Math.random() * 10);
        	}
        }
        System.out.println(two + "+" + three + "=");
    }

    //减法
    public static void createSubtraction() {
    	int one;
        String two, three;
        
        one = (int)(Math.random() * 10);
        if (one < 6) {	//小数减小数
        	int num = (int)(Math.random() * 10);
        	if(num < 5) {	//一位小数减一位小数
        		if((int)(Math.random() * 10) < 5) {
        			two = three = String.format("%.2f", Math.random() * 100);
                    three = String.format("%.1f", Math.random() * 10);
        		} else {
        			two = String.format("%.1f", Math.random() * 10);
        			three = String.format("%.1f", Math.random() * 10);
        		}
        	} else if(num < 8) {	//两位小数减一位小数
        		two = String.format("%.2f", Math.random() * 10);
            	three = String.format("%.1f", Math.random() * 10);
        	} else {	//一位小数减两位小数
        		two = String.format("%.1f", Math.random() * 10);
            	three = String.format("%.2f", Math.random() * 10);
        	}

    		BigDecimal p1 = new BigDecimal(two);
            BigDecimal p2 = new BigDecimal(three);
            System.out.println(p2.add(p1).doubleValue() + "-" + three + "=");
        } else if (one < 8) {	//小数减整数
        	if((int)(Math.random() * 10) < 5) {
        		two = String.format("%.1f", Math.random() * 10);
        		three = String.valueOf((int)(Math.random() * 99) + 1);
        	} else {
        		two = String.format("%.2f", Math.random() * 10);
        		three = String.valueOf((int)(Math.random() * 9) + 1);
        	}

        	BigDecimal p1 = new BigDecimal(two);
            BigDecimal p2 = new BigDecimal(three);
            System.out.println(p2.add(p1).doubleValue() + "-" + three + "=");
        } else {	//整数减小数
        	if((int)(Math.random() * 10) < 5) {
        		two = String.valueOf((int)(Math.random() * 99) + 1);
        		three = String.format("%.1f", Math.random() * 10);
        	} else {
        		two = String.valueOf((int)(Math.random() * 9) + 1);
        		three = String.format("%.2f", Math.random() * 10);
        	}

        	BigDecimal p1 = new BigDecimal(two);
            BigDecimal p2 = new BigDecimal(three);
            System.out.println(p2.add(p1).doubleValue() + "-" + three + "=");
        }
    }    

    public static void main(String[] args) {
        for(int i = 0; i < 20; i++) {	//一共生成20道题
        	int num = (int)(Math.random() * 10);
        	if(num < 4) {	//乘法占40%
        		createMultiplication();
        	} else if(num < 8) {	//除法占40%
        		createDivision();
        	} else if(num < 9) {	//加法占10%
        		createAddition();
        	} else {	//减法占10%
        		createSubtraction();
        	}
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值