像阿超那样,花20分钟写一个能自动生成小学四则运算题目的 “软件”,要求:除了整数以外,还要支持真分数的四则运算。和同学们比较一下各自程序的功能,实现方法的异同,等等。...

本文介绍了一个使用Java实现的数学运算题生成器,该程序能够随机生成加、减、乘、除四则运算题目,并确保运算结果在合理范围内。通过调整参数可以灵活控制题目难度。
public class c30questions {
    public static void main(String[] args) {
        print30Questions();
    }
    private static void print30Questions() {
        //说明:打印30道题函数,把接收到的题目字符串按照指定格式输出。
        for (int i = 0; i < 10; i++) {
            System.out.print( i+1 );
            System.out.print("." + getQuestion() + "    ");
            System.out.print( i+11 );
            System.out.print("." + getQuestion() + "    ");
            System.out.print( i+21 );
            System.out.println("." + getQuestion());
        }
    }
    private static String getQuestion() {
        //说明:getQuestion()得到题目函数,随机生成题目,返回题目字符串。
        int t = 0;
        String strz = "";    
        int x=1+(int)(Math.random()*100);        
        int y=1+(int)(Math.random()*100);    
        int z=1+(int)(Math.random()*100);
        if( z<=25 ) {
            strz = "×";
            x = x % 10;
            y = y % 10;//两个各位数相成
        }
        if( z>25 && z<=50 ) {
            strz = "÷";
            y = ( y % 10 ) + 1;//除数不能为0是不是
        }
        if( z>50 && z<=75) {
            strz = "+";
            if ((x+y) > 100) {        
                x = x / 2;
                y = y / 2;//乘法相加结果不大于100
            }        
        }
        if( z>75 ) {
            strz = "-";
            if( x < y ){
                t = y;
                y = x;
                x = t;//保证减法是大数减去小数
            }
        }            
        String strx = String.valueOf( x );
        String stry = String.valueOf( y );
        String ques = " " + strx + strz + stry + "=";
        if(ques.length()==5)ques = ques + " ";
        if(ques.length()==4)ques = ques + "  ";
        if(ques.length()==3)ques = ques + "   ";//保持格式
        return ques;
    }//getQuestion

}

转载于:https://www.cnblogs.com/zhanghui001/p/5960545.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值