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

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() {
        //得到题目函数,随机生成题目,返回题目字符串
        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;
    }
}
posted @ 2016-10-14 16:15  一只洋洋洋  阅读(242)  评论(0编辑  收藏  举报