zoukankan      html  css  js  c++  java
  • java 加减法2

    一:题目要求:

       1.是否有乘除法

       2.是否有括号

       3.取值范围

       4.加减有无负数

       5.除法有无余数

    二:设计思路:

             在上一个程序的基础之上,按照题目要求,在原有的选择出整数题或者分数题菜单下,分别添加是否有乘除法的下一级菜单,并创建相应的函数。在函数里,通过引用上一次题目的函数,并加以修改,来完成目标。在完成加减有无负数是,在while函数内的最前面添加加减有无负数菜单,把加减有无负数作为第一个菜单,再一级一级的往下进行。对于除法有无余数,在选择完有无乘除法之后,可以选择有无乘除法,然后对除法有无余数进行选择,如果选择了有余数,那么就把上一个程序的代码直接引用,如果选择无余数,那么在原有函数的除法运算基础上,对是否有余数进行判断,如果有余数就重新生成随机数,直至生成无余数的。我编写的程序里的所有功能基本都是利用if  else函数编写的,有些地方我打算利用while函数进行改进。对于未完成的功能,我在运行程序的时候错误太多,正在改进中。

    三:程序代码:

    import java.util.Scanner;
    
    
    public class suanshu {
    
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            int m=0;
            while(m==0)
            {
                System.out.println("加减有无负数");
                System.out.println(" 1.有         ");
                System.out.println(" 2.无         ");
                System.out.println(" 请选择:    ");
                int e = input.nextInt();
                if(e==1)//加减有负数
                {
                    menu();
                    int a = input.nextInt();
                    if(a==1)//整数
                    {
                        System.out.println("是否有乘除法");
                        System.out.println(" 1.是         ");
                        System.out.println(" 2.否         ");
                        System.out.println(" 3.退出      ");
                        System.out.println(" 请选择:    ");
                        int c = input.nextInt();
                        if(c==1)//有乘除法
                        {
                            System.out.println("除法是否有余数");
                            System.out.println(" 1.是         ");
                            System.out.println(" 2.否         ");
                            System.out.println(" 3.退出      ");
                            System.out.println(" 请选择:    ");
                            int f = input.nextInt();
                            if(f==1)//整数除法由余数
                            {
                                System.out.println("请输入出题数量");
                                shu(input.nextInt());
                            }
                            if(f==2)//整数除法没有余数
                            {
                                System.out.println("请输入出题数量");
                                meiyushu(input.nextInt());
                            }
                        }
                        if(c==2)//无乘除法
                        {
                            System.out.println("请输入出题数量");
                            shu1(input.nextInt());
                        }
                        else
                        {
                            System.out.println("已退出");
                            System.exit(0);
                        }
                    }
                    if(a==2)//分数
                    {
                        System.out.println("是否有乘除法");
                        System.out.println(" 1.是         ");
                        System.out.println(" 2.否         ");
                        System.out.println(" 3.退出      ");
                        System.out.println(" 请选择:    ");
                        int d = input.nextInt();
                        if(d==1)//有乘除法
                        {
                            System.out.println("除法是否有余数");
                            System.out.println(" 1.是         ");
                            System.out.println(" 2.否         ");
                            System.out.println(" 3.退出      ");
                            System.out.println(" 请选择:    ");
                            int g = input.nextInt();
                            if(g==1)//除法有余数
                            {
                                System.out.println("输入题目数量");
                                fen(input.nextInt());
                            }
                            if(g==2)//除法无余数
                            {
                                System.out.println("输入题目数量");
                                meiyushufen(input.nextInt());
                            }
                        }
                        if(d==2)//无乘除法
                        {
                            System.out.println("输入题目数量");
                            fen1(input.nextInt());
                        }
                        else
                        {
                            System.out.println("已退出");
                            System.exit(0);
                        }
                    }
                    if(a==3)
                    {
                        System.out.println("已退出");
                        System.exit(0);
                    }
                }
                if(e==2)//加减无负数
                {
                    menu();
                    int a = input.nextInt();
                    if(a==1)//整数
                    {
                        System.out.println("是否有乘除法");
                        System.out.println(" 1.是         ");
                        System.out.println(" 2.否         ");
                        System.out.println(" 3.退出      ");
                        System.out.println(" 请选择:    ");
                        int c = input.nextInt();
                        if(c==1)
                        {
                            System.out.println("请输入出题数量");
                            meiyoufushushu(input.nextInt());
                        }
                        if(c==2)
                        {
                            System.out.println("请输入出题数量");
                            meiyoufushushu1(input.nextInt());
                        }
                        else
                        {
                            System.out.println("已退出");
                            System.exit(0);
                        }
                    }
                    if(a==2)//分数
                    {
                        System.out.println("是否有乘除法");
                        System.out.println(" 1.是         ");
                        System.out.println(" 2.否         ");
                        System.out.println(" 3.退出      ");
                        System.out.println(" 请选择:    ");
                        int d = input.nextInt();
                        if(d==1)
                        {
                            System.out.println("输入题目数量");
                            meiyoufushufen(input.nextInt());
                        }
                        if(d==2)
                        {
                            System.out.println("输入题目数量");
                            meiyoufushufen1(input.nextInt());
                        }
                        else
                        {
                            System.out.println("已退出");
                            System.exit(0);
                        }
                    }
                    if(a==3)
                    {
                        System.out.println("已退出");
                        System.exit(0);
                    }
                  
                }
                }
                
        }
        public static void meiyushufen(int a) {
            for(int b=0;b<a;b++)
            {
                int x = 1+(int)(Math.random()*100);
                int y = 1+(int)(Math.random()*100);
                int m = 1+(int)(Math.random()*100);
                int n = 1+(int)(Math.random()*100);
                int z = 1+(int)(Math.random()*4);
                if(z==1)
                {
                    if(x<y&&m<n)
                    {
                        System.out.println("("+x+"/"+y+")"+"+"+"("+m+"/"+n+")"+"="+"?");
                    }
                    else if(x<y&&m>n)
                    {
                        System.out.println("("+x+"/"+y+")"+"+"+"("+n+"/"+m+")"+"="+"?");
                    }
                    else if(x>y&&m>n)
                    {
                        System.out.println("("+y+"/"+x+")"+"+"+"("+n+"/"+m+")"+"="+"?");
                    }
                    else
                    {
                        System.out.println("("+y+"/"+x+")"+"+"+"("+m+"/"+n+")"+"="+"?");
                    }
                }
                if(z==2)
                {
                    if(x<y&&m<n)
                    {
                        System.out.println("("+x+"/"+y+")"+"-"+"("+m+"/"+n+")"+"="+"?");
                    }
                    else if(x<y&&m>n)
                    {
                        System.out.println("("+x+"/"+y+")"+"-"+"("+n+"/"+m+")"+"="+"?");
                    }
                    else if(x>y&&m>n)
                    {
                        System.out.println("("+y+"/"+x+")"+"-"+"("+n+"/"+m+")"+"="+"?");
                    }
                    else
                    {
                        System.out.println("("+y+"/"+x+")"+"-"+"("+m+"/"+n+")"+"="+"?");
                    }
                }
                if(z==3)
                {
                    if(x<y&&m<n)
                    {
                        System.out.println("("+x+"/"+y+")"+"*"+"("+m+"/"+n+")"+"="+"?");
                    }
                    else if(x<y&&m>n)
                    {
                        System.out.println("("+x+"/"+y+")"+"*"+"("+n+"/"+m+")"+"="+"?");
                    }
                    else if(x>y&&m>n)
                    {
                        System.out.println("("+y+"/"+x+")"+"*"+"("+n+"/"+m+")"+"="+"?");
                    }
                    else
                    {
                        System.out.println("("+y+"/"+x+")"+"*"+"("+m+"/"+n+")"+"="+"?");
                    }
                }
                if(z==4)
                {
                    if(x<y&&m<n)
                    {
                        if((x*n)%(y*m)==0)
                        {
                            System.out.println("("+x+"/"+y+")"+"/"+"("+m+"/"+n+")"+"="+"?");
                        }
                    }
                    else if(x<y&&m>n)
                    {
                        if((x*m)%(y*n)==0)
                        {
                            System.out.println("("+x+"/"+y+")"+"/"+"("+n+"/"+m+")"+"="+"?");
                        }
                    }
                    else if(x>y&&m>n)
                    {
                        if((y*m)%(x*n)==0)
                        {
                            System.out.println("("+y+"/"+x+")"+"/"+"("+n+"/"+m+")"+"="+"?");
                        }
                    }
                    else
                    {
                        if((y*n)%(x*m)==0)
                        {
                            System.out.println("("+y+"/"+x+")"+"/"+"("+m+"/"+n+")"+"="+"?");
                        }
                    }
                }
            }
        }
        public static void meiyushu(int a) {
            for(int b=0;b<a;b++)
            {
                int x = 1+(int)(Math.random()*100);
                int y = 1+(int)(Math.random()*100);
                int z = 1+(int)(Math.random()*4);
                if(z==1)
                {
                    System.out.println(x+"+"+y+"="+"?");
                }
                if(z==2)
                {
                        System.out.println(x+"-"+y+"="+"?");    
                }
                if(z==3)
                {
                    System.out.println(x+"*"+y+"="+"?");
                }
                if(z==4)
                {
                    if(x%y==0)
                    {
                        System.out.println(x+"/"+y+"="+"?");
                    }
                }
            }
        }
        private static void meiyoufushufen1(int a) {
            for(int b=0;b<a;b++)
            {
                int x = 1+(int)(Math.random()*100);
                int y = 1+(int)(Math.random()*100);
                int m = 1+(int)(Math.random()*100);
                int n = 1+(int)(Math.random()*100);
                int z = 1+(int)(Math.random()*2);
                if(z==1)
                {
                    if(x<y&&m<n)
                    {
                        System.out.println("("+x+"/"+y+")"+"+"+"("+m+"/"+n+")"+"="+"?");
                    }
                    else if(x<y&&m>n)
                    {
                        System.out.println("("+x+"/"+y+")"+"+"+"("+n+"/"+m+")"+"="+"?");
                    }
                    else if(x>y&&m>n)
                    {
                        System.out.println("("+y+"/"+x+")"+"+"+"("+n+"/"+m+")"+"="+"?");
                    }
                    else
                    {
                        System.out.println("("+y+"/"+x+")"+"+"+"("+m+"/"+n+")"+"="+"?");
                    }
                }
                if(z==2)
                {
                    if(x<y&&m<n)
                    {
                        if(x*n>y*m)
                        {
                            System.out.println("("+x+"/"+y+")"+"-"+"("+m+"/"+n+")"+"="+"?");
                        }
                        else
                        {
                            System.out.println("("+m+"/"+n+")"+"-"+"("+x+"/"+y+")"+"="+"?");
                        }
                    }
                    else if(x<y&&m>n)
                    {
                        if(x*m>y*n)
                        {
                            System.out.println("("+x+"/"+y+")"+"-"+"("+n+"/"+m+")"+"="+"?");
                        }
                        else
                        {
                            System.out.println("("+n+"/"+m+")"+"-"+"("+x+"/"+y+")"+"="+"?");
                        }
                    }
                    else if(x>y&&m>n)
                    {
                        if(y*m>n*x)
                        {
                            System.out.println("("+y+"/"+x+")"+"-"+"("+n+"/"+m+")"+"="+"?");
                        }
                        else
                        {
                            System.out.println("("+n+"/"+m+")"+"-"+"("+y+"/"+x+")"+"="+"?");
                        }
                    }
                    else
                    {
                        if(y*n>m*x)
                        {
                            System.out.println("("+y+"/"+x+")"+"-"+"("+m+"/"+n+")"+"="+"?");
                        }
                        else
                        {
                            System.out.println("("+x+"/"+m+")"+"-"+"("+y+"/"+x+")"+"="+"?");
                        }
                    }
                }
                
            }
        }
        private static void meiyoufushufen(int a) {
            int x = 1+(int)(Math.random()*100);
            int y = 1+(int)(Math.random()*100);
            int m = 1+(int)(Math.random()*100);
            int n = 1+(int)(Math.random()*100);
            int z = 1+(int)(Math.random()*4);
            if(z==1)
            {
                if(x<y&&m<n)
                {
                    System.out.println("("+x+"/"+y+")"+"+"+"("+m+"/"+n+")"+"="+"?");
                }
                else if(x<y&&m>n)
                {
                    System.out.println("("+x+"/"+y+")"+"+"+"("+n+"/"+m+")"+"="+"?");
                }
                else if(x>y&&m>n)
                {
                    System.out.println("("+y+"/"+x+")"+"+"+"("+n+"/"+m+")"+"="+"?");
                }
                else
                {
                    System.out.println("("+y+"/"+x+")"+"+"+"("+m+"/"+n+")"+"="+"?");
                }
            }
            if(z==2)
            {
                if(x<y&&m<n)
                {
                    if(x*n>y*m)
                    {
                        System.out.println("("+x+"/"+y+")"+"-"+"("+m+"/"+n+")"+"="+"?");
                    }
                    else
                    {
                        System.out.println("("+m+"/"+n+")"+"-"+"("+x+"/"+y+")"+"="+"?");
                    }
                }
                else if(x<y&&m>n)
                {
                    if(x*m>y*n)
                    {
                        System.out.println("("+x+"/"+y+")"+"-"+"("+n+"/"+m+")"+"="+"?");
                    }
                    else
                    {
                        System.out.println("("+n+"/"+m+")"+"-"+"("+x+"/"+y+")"+"="+"?");
                    }
                }
                else if(x>y&&m>n)
                {
                    if(y*m>n*x)
                    {
                        System.out.println("("+y+"/"+x+")"+"-"+"("+n+"/"+m+")"+"="+"?");
                    }
                    else
                    {
                        System.out.println("("+n+"/"+m+")"+"-"+"("+y+"/"+x+")"+"="+"?");
                    }
                }
                else
                {
                    if(y*n>m*x)
                    {
                        System.out.println("("+y+"/"+x+")"+"-"+"("+m+"/"+n+")"+"="+"?");
                    }
                    else
                    {
                        System.out.println("("+x+"/"+m+")"+"-"+"("+y+"/"+x+")"+"="+"?");
                    }
                }
            }
            if(z==3)
            {
                if(x<y&&m<n)
                {
                    System.out.println("("+x+"/"+y+")"+"*"+"("+m+"/"+n+")"+"="+"?");
                }
                else if(x<y&&m>n)
                {
                    System.out.println("("+x+"/"+y+")"+"*"+"("+n+"/"+m+")"+"="+"?");
                }
                else if(x>y&&m>n)
                {
                    System.out.println("("+y+"/"+x+")"+"*"+"("+n+"/"+m+")"+"="+"?");
                }
                else
                {
                    System.out.println("("+y+"/"+x+")"+"*"+"("+m+"/"+n+")"+"="+"?");
                }
            }
            if(z==4)
            {
                if(x<y&&m<n)
                {
                    System.out.println("("+x+"/"+y+")"+"/"+"("+m+"/"+n+")"+"="+"?");
                }
                else if(x<y&&m>n)
                {
                    System.out.println("("+x+"/"+y+")"+"/"+"("+n+"/"+m+")"+"="+"?");
                }
                else if(x>y&&m>n)
                {
                    System.out.println("("+y+"/"+x+")"+"/"+"("+n+"/"+m+")"+"="+"?");
                }
                else
                {
                    System.out.println("("+y+"/"+x+")"+"/"+"("+m+"/"+n+")"+"="+"?");
                }
            }
        
        }
        private static void meiyoufushushu1(int a) {
            for(int b=0;b<a;b++)
            {
                int x = 1+(int)(Math.random()*100);
                int y = 1+(int)(Math.random()*100);
                int z = 1+(int)(Math.random()*2);
                if(z==1)
                {
                    System.out.println(x+"+"+y+"="+"?");
                }
                if(z==2)
                {
                    if(x>y)
                    {
                        System.out.println(x+"-"+y+"="+"?");
                    }
                    else
                    {
                        System.out.println(y+"-"+x+"="+"?");
                    }
                }
            }
        }
        private static void meiyoufushushu(int a) {
            for(int b=0;b<a;b++)
            {
                int x = 1+(int)(Math.random()*100);
                int y = 1+(int)(Math.random()*100);
                int z = 1+(int)(Math.random()*4);
                if(z==1)
                {
                    System.out.println(x+"+"+y+"="+"?");
                }
                if(z==2)
                {
                    if(x>y)
                    {
                        System.out.println(x+"-"+y+"="+"?");
                    }
                    else
                    {
                        System.out.println(y+"-"+x+"="+"?");
                    }
                }
                if(z==3)
                {
                    System.out.println(x+"*"+y+"="+"?");
                }
                if(z==4)
                {
                    System.out.println(x+"/"+y+"="+"?");
                }
            }
        }
        public static void menu()
        {
            System.out.println("***************");
            System.out.println("请输入要选择的选项:");
            System.out.println(" 1.整数              ");
            System.out.println(" 2.分数              ");
            System.out.println(" 3.退出              ");
            System.out.println("***************");
            System.out.println(" 请选择:");
        }
        public static void fen1(int a) {
            for(int b=0;b<a;b++)
            {
                int x = 1+(int)(Math.random()*100);
                int y = 1+(int)(Math.random()*100);
                int m = 1+(int)(Math.random()*100);
                int n = 1+(int)(Math.random()*100);
                int z = 1+(int)(Math.random()*2);
                if(z==1)
                {
                    if(x<y&&m<n)
                    {
                        System.out.println("("+x+"/"+y+")"+"+"+"("+m+"/"+n+")"+"="+"?");
                    }
                    else if(x<y&&m>n)
                    {
                        System.out.println("("+x+"/"+y+")"+"+"+"("+n+"/"+m+")"+"="+"?");
                    }
                    else if(x>y&&m>n)
                    {
                        System.out.println("("+y+"/"+x+")"+"+"+"("+n+"/"+m+")"+"="+"?");
                    }
                    else
                    {
                        System.out.println("("+y+"/"+x+")"+"+"+"("+m+"/"+n+")"+"="+"?");
                    }
                }
                if(z==2)
                {
                    if(x<y&&m<n)
                    {
                        System.out.println("("+x+"/"+y+")"+"-"+"("+m+"/"+n+")"+"="+"?");
                    }
                    else if(x<y&&m>n)
                    {
                        System.out.println("("+x+"/"+y+")"+"-"+"("+n+"/"+m+")"+"="+"?");
                    }
                    else if(x>y&&m>n)
                    {
                        System.out.println("("+y+"/"+x+")"+"-"+"("+n+"/"+m+")"+"="+"?");
                    }
                    else
                    {
                        System.out.println("("+y+"/"+x+")"+"-"+"("+m+"/"+n+")"+"="+"?");
                    }
                }
                
            }
        }
        public static void shu1(int a) {//没有乘除法
            for(int b=0;b<a;b++)
            {
                int x = 1+(int)(Math.random()*100);
                int y = 1+(int)(Math.random()*100);
                int z = 1+(int)(Math.random()*2);
                if(z==1)
                {
                    System.out.println(x+"+"+y+"="+"?");
                }
                if(z==2)
                {
                    System.out.println(x+"-"+y+"="+"?");
                }
            }
        }
        public static void fen(int a) {
            for(int b=0;b<a;b++)
            {
                int x = 1+(int)(Math.random()*100);
                int y = 1+(int)(Math.random()*100);
                int m = 1+(int)(Math.random()*100);
                int n = 1+(int)(Math.random()*100);
                int z = 1+(int)(Math.random()*4);
                if(z==1)
                {
                    if(x<y&&m<n)
                    {
                        System.out.println("("+x+"/"+y+")"+"+"+"("+m+"/"+n+")"+"="+"?");
                    }
                    else if(x<y&&m>n)
                    {
                        System.out.println("("+x+"/"+y+")"+"+"+"("+n+"/"+m+")"+"="+"?");
                    }
                    else if(x>y&&m>n)
                    {
                        System.out.println("("+y+"/"+x+")"+"+"+"("+n+"/"+m+")"+"="+"?");
                    }
                    else
                    {
                        System.out.println("("+y+"/"+x+")"+"+"+"("+m+"/"+n+")"+"="+"?");
                    }
                }
                if(z==2)
                {
                    if(x<y&&m<n)
                    {
                        System.out.println("("+x+"/"+y+")"+"-"+"("+m+"/"+n+")"+"="+"?");
                    }
                    else if(x<y&&m>n)
                    {
                        System.out.println("("+x+"/"+y+")"+"-"+"("+n+"/"+m+")"+"="+"?");
                    }
                    else if(x>y&&m>n)
                    {
                        System.out.println("("+y+"/"+x+")"+"-"+"("+n+"/"+m+")"+"="+"?");
                    }
                    else
                    {
                        System.out.println("("+y+"/"+x+")"+"-"+"("+m+"/"+n+")"+"="+"?");
                    }
                }
                if(z==3)
                {
                    if(x<y&&m<n)
                    {
                        System.out.println("("+x+"/"+y+")"+"*"+"("+m+"/"+n+")"+"="+"?");
                    }
                    else if(x<y&&m>n)
                    {
                        System.out.println("("+x+"/"+y+")"+"*"+"("+n+"/"+m+")"+"="+"?");
                    }
                    else if(x>y&&m>n)
                    {
                        System.out.println("("+y+"/"+x+")"+"*"+"("+n+"/"+m+")"+"="+"?");
                    }
                    else
                    {
                        System.out.println("("+y+"/"+x+")"+"*"+"("+m+"/"+n+")"+"="+"?");
                    }
                }
                if(z==4)
                {
                    if(x<y&&m<n)
                    {
                        System.out.println("("+x+"/"+y+")"+"/"+"("+m+"/"+n+")"+"="+"?");
                    }
                    else if(x<y&&m>n)
                    {
                        System.out.println("("+x+"/"+y+")"+"/"+"("+n+"/"+m+")"+"="+"?");
                    }
                    else if(x>y&&m>n)
                    {
                        System.out.println("("+y+"/"+x+")"+"/"+"("+n+"/"+m+")"+"="+"?");
                    }
                    else
                    {
                        System.out.println("("+y+"/"+x+")"+"/"+"("+m+"/"+n+")"+"="+"?");
                    }
                }
            }
            
        }
        public static void shu(int a)
        {
            for(int b=0;b<a;b++)
            {
                int x = 1+(int)(Math.random()*100);
                int y = 1+(int)(Math.random()*100);
                int z = 1+(int)(Math.random()*4);
                if(z==1)
                {
                    System.out.println(x+"+"+y+"="+"?");
                }
                if(z==2)
                {
                        System.out.println(x+"-"+y+"="+"?");
                }
                if(z==3)
                {
                    System.out.println(x+"*"+y+"="+"?");
                }
                if(z==4)
                {
                    System.out.println(x+"/"+y+"="+"?");
                }
            }
        }
    }

    四:程序运行截图:

     

  • 相关阅读:
    建站始末——(转载)
    阿里云——大神建个人网站分享(转载)
    从零开始建设个人网站
    前端资料——书籍
    【python】*与** 参数问题
    【python】python异常类型
    【python】unittest中常用的assert语句
    【性能测试】性能测试总结<四>
    【性能测试】性能测试总结<三>
    【性能测试】性能测试总结<二>
  • 原文地址:https://www.cnblogs.com/gong123/p/6532556.html
Copyright © 2011-2022 走看看