四则运算02

设计思想:

把整数和真分数进行分类,在每个分类下进行输入题目量、数值范围、是否有乘除法、加减法是否有负数、除法是否有余数的操作。

在整数下定义两个随机数,在真分数下定义四个随机数。

如果有乘除法,加减法有负数,除法有余数就直接输出题目。

如果无乘除法,那么输出的结果就只有加减法的题目。

源程序代码:

package test;

import java.util.Random;
import java.util.Scanner;

public class yunsuan {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int a,b,c,m,n,x,min,max;
        Scanner sc = new Scanner(System.in);
        System.out.println("请选择整数还是真分数(整数输入1.真分数输入2):");
        x = sc.nextInt();
        if(x==1){
        System.out.println("请输入要打印题目的数量:");
        m = sc.nextInt();
        System.out.println("请输入数值的范围(最小值和最大值):");
        min = sc.nextInt();
        max = sc.nextInt();
        System.out.println("请输入是否有乘除法:(有输入1.反之输入2)");
        a = sc.nextInt();
        System.out.println("请输入加减法是否有负数:(有输入1.反之输入2)");
        b = sc.nextInt();
        System.out.println("请输入除法是否有余数:(有输入1.反之输入2)");
        c = sc.nextInt();
        sc.close();
        if(a==1)//有乘除法
        {
            if(b==1)//有乘除法。加减法有负数
            {
                if(c==1)//有乘除法。加减法有负数。除法有余数
                {
                    for(int i=0;i<m;i++)
                    {
                        Random rand = new Random();
                        int firstnum = rand.nextInt(max)+min;
                        int secondnum = rand.nextInt(max)+min;
                        int num = rand.nextInt(4)+1;
                        if(num==1)
                            System.out.print(firstnum+" + "+secondnum+" =       ");
                        if(num==2)
                            System.out.print(firstnum+" - "+secondnum+" =       ");
                        if(num==3)
                            System.out.print(firstnum+" * "+secondnum+" =       ");
                        if(num==4)
                            System.out.print(firstnum+" / "+secondnum+" =       ");
                        if(i%5==0){
                            System.out.println();
                        }
                    }
                }
                if(c==2)//有乘除法。加减法有负数。除法无余数
                {
                    for(int i=0;i<m;i++)
                    {
                        Random rand = new Random();
                        int firstnum = rand.nextInt(max)+min;
                        int secondnum = rand.nextInt(max)+min;
                        int num = rand.nextInt(4)+1;
                        if(num==1)
                            System.out.print(firstnum+" + "+secondnum+" =       ");
                        if(num==2)
                            System.out.print(firstnum+" - "+secondnum+" =       ");
                        if(num==3)
                            System.out.print(firstnum+" * "+secondnum+" =       ");
                        if(num==4){
                            if(firstnum%secondnum!=0)
                            {
                                i=i-1;
                            }
                            else{System.out.print(firstnum+" / "+secondnum+" =       ");}

                        }
                        if(i%5==0){
                            System.out.println();
                        }
                    }
                }
            }
            if(b==2)//有乘除法。加减法无负数
            {
                if(c==1)//有乘除法。加减法无负数。除法有余数
                {
                    for(int i=0;i<m;i++)
                    {
                        Random rand = new Random();
                        int firstnum = rand.nextInt(max)+min;
                        int secondnum = rand.nextInt(max)+min;
                        int num = rand.nextInt(4)+1;
                        if(num==1)
                            System.out.print(firstnum+" + "+secondnum+" =       ");
                        if(num==2)
                        {
                            int l;
                            l=firstnum-secondnum;
                            if(l<0){
                                i=i-1;
                            }
                            if(l>=0){
                                System.out.print(firstnum+" - "+secondnum+" =       ");
                            }
                        }
                        if(num==3)
                            System.out.print(firstnum+" * "+secondnum+" =       ");
                        if(num==4)
                            System.out.print(firstnum+" / "+secondnum+" =       ");
                        if(i%5==0){
                            System.out.println();
                        }
                    }
                }
                if(c==2)//有乘除法。加减法无负数。除法无余数
                {
                    for(int i=0;i<m;i++)
                    {
                        Random rand = new Random();
                        int firstnum = rand.nextInt(max)+min;
                        int secondnum = rand.nextInt(max)+min;
                        int num = rand.nextInt(4)+1;
                        if(num==1)
                            System.out.print(firstnum+" + "+secondnum+" =       ");
                        if(num==2)
                        {
                            int l;
                            l=firstnum-secondnum;
                            if(l<0){
                                i=i-1;
                            }
                            if(l>=0){
                                System.out.print(firstnum+" - "+secondnum+" =       ");
                            }
                        }
                        if(num==3)
                            System.out.print(firstnum+" * "+secondnum+" =       ");
                        if(num==4){
                            if(firstnum%secondnum!=0)
                            {
                                i=i-1;
                            }
                            else{System.out.print(firstnum+" / "+secondnum+" =       ");}

                    }
                        if(i%5==0){
                            System.out.println();
                        }
                }
            }
        }
        }
        if(a==2)//无乘除法
        {
            if(b==1)//没有乘除法,加减法有负数
            {
                if(c==1||c==2){
                for(int i=0;i<m;i++)
                {
                    Random rand = new Random();
                    int firstnum = rand.nextInt(max)+min;
                    int secondnum = rand.nextInt(max)+min;
                    int num = rand.nextInt(2)+1;
                    if(num==1)
                        System.out.print(firstnum+" + "+secondnum+" =       ");
                    if(num==2)
                        System.out.print(firstnum+" - "+secondnum+" =       ");
                    if(i%5==0){
                        System.out.println();
                    }
                }
                }

            }
            if(b==2)//没有乘除法,加减法无负数
            {
                if(c==1||c==2){
                for(int i=0;i<m;i++)
                {
                    Random rand = new Random();
                    int firstnum = rand.nextInt(max)+min;
                    int secondnum = rand.nextInt(max)+min;
                    int num = rand.nextInt(2)+1;
                    if(num==1){
                        System.out.print(firstnum+" + "+secondnum+" =       ");}
                    if(num==2)
                    {
                        int l;
                        l=firstnum-secondnum;
                        if(l<0){
                            i=i-1;
                        }
                        if(l>=0){
                            System.out.print(firstnum+" - "+secondnum+" =       ");
                        }
                    }
                    if(i%5==0){
                        System.out.println();
                    }
                }
                }
            }

        }

    }

        if(x==2){
            System.out.println("请输入要打印题目的数量:");
            m = sc.nextInt();
            System.out.println("请输入数值的范围(最小值和最大值):");
            min = sc.nextInt();
            max = sc.nextInt();
            System.out.println("请输入是否有乘数法:(有输入1.反之输入2)");
            a = sc.nextInt();
            System.out.println("请输入加减法是否有负数:(有输入1.反之输入2)");
            b = sc.nextInt();
            sc.close();
            if(a==1)//有乘除法
            {
                if(b==1)//有乘除法,加减法有负数
                {
                        for(int i=0;i<m;i++)
                        {
                            Random rand = new Random();
                            int firstnum = rand.nextInt(max)+min;
                            int secondnum = rand.nextInt(max)+min;
                            int thirdnum = rand.nextInt(max)+min;
                            int fourthnum = rand.nextInt(max)+min;
                            int num = rand.nextInt(4)+1;
                            int flag = 0;
                            if(firstnum>secondnum){
                                int temp;
                                temp = firstnum;
                                firstnum = secondnum;
                                secondnum = temp;
                            }
                            if(thirdnum>fourthnum){
                                int temp;
                                temp = thirdnum;
                                thirdnum = fourthnum;
                                fourthnum = temp;
                            }//如果分子大于分母,则分子分母交换位置
                            if(firstnum==secondnum&&thirdnum==fourthnum){
                                flag=1;
                            }
                            if(firstnum==secondnum&&thirdnum!=fourthnum){
                                flag=1;
                            }
                            if(firstnum!=secondnum&&thirdnum==fourthnum){
                                flag=1;
                            }
                            if(flag==1){
                                i=i-1;
                            }//如果分子分母相等,则重新生成
                            else{
                            if(num==1)
                                System.out.print(firstnum+"/"+secondnum+" + "+thirdnum+"/"+fourthnum+" =       ");
                            if(num==2)
                                System.out.print(firstnum+"/"+secondnum+" - "+thirdnum+"/"+fourthnum+" =       ");
                            if(num==3)
                                System.out.print(firstnum+"/"+secondnum+" * "+thirdnum+"/"+fourthnum+" =       ");
                            if(num==4)
                                System.out.print(firstnum+"/"+secondnum+" / "+thirdnum+"/"+fourthnum+" =       ");

                            if(i%5==0){
                                System.out.println();
                            }
                        }
                            }
                    }

                if(b==2)//有乘除法,加减法无负数
                {
                        for(int i=0;i<m;i++)
                        {
                            Random rand = new Random();
                            int firstnum = rand.nextInt(max)+min;
                            int secondnum = rand.nextInt(max)+min;
                            int thirdnum = rand.nextInt(max)+min;
                            int fourthnum = rand.nextInt(max)+min;
                            int num = rand.nextInt(4)+1;
                            int flag=0;
                            if(firstnum>secondnum){
                                int temp;
                                temp = firstnum;
                                firstnum = secondnum;
                                secondnum = temp;
                            }
                            if(thirdnum>fourthnum){
                                int temp;
                                temp = thirdnum;
                                thirdnum = fourthnum;
                                fourthnum = temp;
                            }
                            if(firstnum==secondnum&&thirdnum==fourthnum){
                                flag=1;
                            }
                            if(firstnum==secondnum&&thirdnum!=fourthnum){
                                flag=1;
                            }
                            if(firstnum!=secondnum&&thirdnum==fourthnum){
                                flag=1;
                            }
                            if(flag==1){
                                i=i-1;
                            }
                            else{
                            if(num==1)
                                System.out.print(firstnum+"/"+secondnum+" + "+thirdnum+"/"+fourthnum+" =       ");
                            if(num==2)
                            {
                                int l;
                                l=firstnum/secondnum-thirdnum/fourthnum;
                                if(l<0){
                                    i=i-1;
                                }
                                if(l>=0){
                                    System.out.print(firstnum+"/"+secondnum+" - "+thirdnum+"/"+fourthnum+" =       ");
                                }
                            }
                            if(num==3)
                                System.out.print(firstnum+"/"+secondnum+" * "+thirdnum+"/"+fourthnum+" =       ");
                            if(num==4)
                                System.out.print(firstnum+"/"+secondnum+" / "+thirdnum+"/"+fourthnum+" =       ");
                            if(i%5==0){
                                System.out.println();
                            }
                        }
                        }

            }
            }
            if(a==2)//无乘除法
            {
                if(b==1)//没有乘除法,加减法有负数
                {
                    for(int i=0;i<m;i++)
                    {
                        Random rand = new Random();
                        int firstnum = rand.nextInt(max)+min;
                        int secondnum = rand.nextInt(max)+min;
                        int thirdnum = rand.nextInt(max)+min;
                        int fourthnum = rand.nextInt(max)+min;
                        int num = rand.nextInt(2)+1;
                        int flag=0;
                        if(firstnum>secondnum){
                            int temp;
                            temp = firstnum;
                            firstnum = secondnum;
                            secondnum = temp;
                        }
                        if(thirdnum>fourthnum){
                            int temp;
                            temp = thirdnum;
                            thirdnum = fourthnum;
                            fourthnum = temp;
                        }
                        if(firstnum==secondnum&&thirdnum==fourthnum){
                            flag=1;
                        }
                        if(firstnum==secondnum&&thirdnum!=fourthnum){
                            flag=1;
                        }
                        if(firstnum!=secondnum&&thirdnum==fourthnum){
                            flag=1;
                        }
                        if(flag==1){
                            i=i-1;
                        }
                        else{
                        if(num==1)
                            System.out.print(firstnum+"/"+secondnum+" + "+thirdnum+"/"+fourthnum+" =       ");
                        if(num==2)
                            System.out.print(firstnum+"/"+secondnum+" - "+thirdnum+"/"+fourthnum+" =       ");
                        if(i%5==0){
                            System.out.println();
                        }
                    }
                    }

                }
                if(b==2)//无乘除法,加减法无负数
                {
                    for(int i=0;i<m;i++)
                    {
                        Random rand = new Random();
                        int firstnum = rand.nextInt(max)+min;
                        int secondnum = rand.nextInt(max)+min;
                        int thirdnum = rand.nextInt(max)+min;
                        int fourthnum = rand.nextInt(max)+min;
                        int num = rand.nextInt(2)+1;
                        int flag=0;
                        if(firstnum>secondnum){
                            int temp;
                            temp = firstnum;
                            firstnum = secondnum;
                            secondnum = temp;
                        }
                        if(thirdnum>fourthnum){
                            int temp;
                            temp = thirdnum;
                            thirdnum = fourthnum;
                            fourthnum = temp;
                        }
                        if(firstnum==secondnum&&thirdnum==fourthnum){
                            flag=1;
                        }
                        if(firstnum==secondnum&&thirdnum!=fourthnum){
                            flag=1;
                        }
                        if(firstnum!=secondnum&&thirdnum==fourthnum){
                            flag=1;
                        }
                        if(flag==1){
                            i=i-1;
                        }
                        else{
                        if(num==1)
                            System.out.print(firstnum+"/"+secondnum+" + "+thirdnum+"/"+fourthnum+" =       ");
                        if(num==2)
                        {
                            int l;
                            l=firstnum/secondnum-thirdnum/fourthnum;
                            if(l<0){
                                i=i-1;
                            }
                            if(l>=0){
                                System.out.print(firstnum+"/"+secondnum+" - "+thirdnum+"/"+fourthnum+" =       ");
                            }
                        }
                        if(i%5==0){
                            System.out.println();
                        }
                    }
                    }

                }
            }

    }
    }
}

运行结果截图:

实验总结:

首先,没有完整的完成实验,其中本程序并没有完成“是否有括号”这个参数,本程序只能单纯的输出题目还不能进行答案验证,还需后续的修改。其中我觉得比较有难度的是关于真分数部分真分数的设置,真分数为分子小于分母的分数。还有就是我定义的分子分母是int类型的,不知道有没有错误。待改进。

时间: 2024-09-29 08:49:49

四则运算02的相关文章

四则运算02 PSP项目计划

日期             周一             周二             周三             周四             周五             周六             周日             周总结            

百度刚放假啊数据库风口浪尖萨拉疯了

http://www.ebay.com/cln/l_x5585/2015.02.11/176746639012 http://www.ebay.com/cln/jiacha_boryk/2015.02.11/176837188016 http://www.ebay.com/cln/gbnlin0/2015.02.11/176837189016 http://www.ebay.com/cln/j_j2841/2015.02.11/177066749015 http://www.ebay.com/c

百度房间爱师傅卡卡是快乐疯了;爱死

http://www.ebay.com/cln/shx9479/-/177007606013/2015.02.11 http://www.ebay.com/cln/genqi12/-/176846034010/2015.02.11 http://www.ebay.com/cln/seyyon2/-/176906811016/2015.02.11 http://www.ebay.com/cln/wcn5971/-/176846032010/2015.02.11 http://www.ebay.co

百度和房价是否健康教案上开发

http://www.ebay.com/cln/l.kuan2/-/167247714018/2015.02.10 http://www.ebay.com/cln/setlia-3616/-/167086016019/2015.02.10 http://www.ebay.com/cln/pen-y77/-/167086017019/2015.02.10 http://www.ebay.com/cln/yua-me2/-/167399441016/2015.02.10 http://www.eba

百度电话费健身房拉伸件礼服加拉斯减肥

http://www.ebay.com/cln/cnli_c90nphs5e/-/167379958016/2015.02.07 http://www.ebay.com/cln/gaw4612/-/167226239018/2015.02.07 http://www.ebay.com/cln/re_len4/-/167263594010/2015.02.07 http://www.ebay.com/cln/ta.ku83/-/167162702017/2015.02.07 http://www.

百度回复金卡是减肥拉进来收付款

http://www.ebay.com/cln/cnli_c90nphs5e/-/167379958016/2015.02.08 http://www.ebay.com/cln/gaw4612/-/167226239018/2015.02.08 http://www.ebay.com/cln/re_len4/-/167263594010/2015.02.08 http://www.ebay.com/cln/ta.ku83/-/167162702017/2015.02.08 http://www.

百度放假哈萨克就发了设计费拉萨

http://www.ebay.com/cln/ldicn.mz6dm/2015.02.11/177030163015 http://www.ebay.com/cln/tan_qi5/2015.02.11/176903144013 http://www.ebay.com/cln/l.lu104/2015.02.11/177030175015 http://www.ebay.com/cln/ya01191/2015.02.11/176722580014 http://www.ebay.com/cl

百度房间撒谎发卡上就发了空间啊

http://www.ebay.com/cln/h-h4129/2015.02.11/176819191016 http://www.ebay.com/cln/fendo88/2015.02.11/176613943017 http://www.ebay.com/cln/ygon288/2015.02.11/176727517018 http://www.ebay.com/cln/ta.ch17/2015.02.11/176613950017 http://www.ebay.com/cln/g-

百度房间沙发客服就考试考几分离开

http://www.ebay.com/cln/jinlon8/book/167309734010/2015.02.10 http://www.ebay.com/cln/bam5330/book/167115292019/2015.02.10 http://www.ebay.com/cln/yi_za70/book/167315676012/2015.02.10 http://www.ebay.com/cln/y.y3463/book/167285977014/2015.02.10 http:/