码迷,mamicode.com
首页 > 其他好文 > 详细

四则运算第三次

时间:2017-03-17 19:18:17      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:divide   判断   bsp   span   system.in   exti   ==   static   next   

import java.util.*;
//正数的随机取值
class number1
{
    private int i;
    private int j;
    public void seti()
    {
        i = 1 + (int)(Math.random() * 100);
    }
    public int geti()
    {
        return i;
    }
    public void setj()
    {
        j = 1 + (int)(Math.random() * 99);
    }
    public int getj()
    {
        return j;
    }
    
}
//负数的随机取值
class Number
{
    private int i;
    private int j;
    public void seti()
    {
        i = (-1) + (int)(Math.random() * (-100));
    }
    public int geti()
    {
        return i;
    }
    public void setj()
    {
        j = (-1) + (int)(Math.random() * (-99));
    }
    public int getj()
    {
        return j;
    }
    
}
//运算符号的随机取值
class symbol
{
    private int a;
    public void seta()
    {
        a = 1 + (int)(Math.random() * 4);
    }
    public int geta()
    {
        return a;
    }
}
//分数的随机取值
class number2
{
    private int i;
    private int j;
    private int x;
    private int y;
    public void setnumber()
    {
        i = 1 + (int)(Math.random() * 100);
        j = 1 + (int)(Math.random() * 99);
        x = 1 + (int)(Math.random() * 100);
        y = 1 + (int)(Math.random() * 99);
    }
    public int getnumberi()
    {
        return i;
    }
    public int getnumberj()
    {
        return j;
    }
    public int getnumberx()
    {
        return x;
    }
    public int getnumbery()
    {
        return y;
    }    
}
//混合运算的取值
class number3
{
    private int i;
    private int j;
    private int x;
    private int y;
    public void setnumber()
    {
        i = 1 + (int)(Math.random() * 100);
        j = 1 + (int)(Math.random() * 100);
        x = 1 + (int)(Math.random() * 100);
        y = 1 + (int)(Math.random() * 100);
    }
    public int getnumberi()
    {
        return i;
    }
    public int getnumberj()
    {
        return j;
    }
    public int getnumberx()
    {
        return x;
    }
    public int getnumbery()
    {
        return y;
    }    
}
//混合运算的符号
class symbol1
{
    private int a;
    public void seta()
    {
        a = 1 + (int)(Math.random() * 2);
    }
    public int geta()
    {
        return a;
    }
}
//主函数
public class calculate {
    public static void main(String args[])
    {
        while(true)
        {
            menu();
            System.out.println("请输入想要运行的计算");
            Scanner scan = new Scanner(System.in);
            int p;
            p = scan.nextInt();
            if(p == 1)
            {
                positive();
            }
            if(p == 2)
            {
                negative();
            }
            if(p == 3)
            {
                fraction();
            }
                
            if(p == 4)
            {
                divide();
            }
            if(p == 5)
            {
                divide1();
            }
            if(p == 6)
            {
                range();
            }
            if(p == 7)
            {
                mixture();
            }
        }
    }
    public static String simplify(int a,int b)   //获得最简分数
    {
        int max = 0;
        for(int i = 0 ;i < 100 ;i ++)
        {
            int n = i + 1 ;
            if(a % n == 0 && b % n == 0)
                max = n;
            
        }
        return (String)( a / max + "/" + b / max);
    }
    public static int charge(int answer) //判断正误
    {
        System.out.println("请输入您的答案:");
        Scanner in = new Scanner(System.in);
        int i;
        i = in.nextInt();
        if(i == answer)
        {
            return 1;
        }
        else return 0;
    }
     public static void menu()
    {
        System.out.println("1. 正数的四则运算");
        System.out.println("2. 负数的四则运算");
        System.out.println("3. 分数的四则运算");
        System.out.println("4. 整除运算");
        System.out.println("5. 非整除运算");
        System.out.println("6. 规定数值范围的运算");
        System.out.println("7. 混合运算");
    }
    public static void positive() //正数运算
    {
        for(int i = 0 ; i <= 29 ; i ++){
            number1 n1 = new number1();
            int a1 , a2;
            n1.seti();
            a1 = n1.geti();
            n1.setj();
            a2 = n1.getj();
            symbol s = new symbol();
            int b;
            s.seta();
            b = s.geta();
            if(b == 1)
                System.out.println(a1 + "+" + a2 + "=" + ( a1 + a2 ) );
            if(b == 2)
                System.out.println(a1 + "-" + a2 + "=" + (a1 - a2));
            if(b == 3)
                System.out.println(a1 + "*" + a2 + "=" + (a1 * a2));
            if(b == 4)
            {
                int c = a1 % a2;
                int c1 = (a1 - c) / a2;
                System.out.println(a1 + "/" + a2 + "=" + c1 + "余" + c);
            }
                
        }
        
    }
    public static void negative() //负数运算
    {
        for(int i = 0 ; i <= 29 ; i ++){
            Number n1 = new Number();
            int a1 , a2;
            n1.seti();
            a1 = n1.geti();
            n1.setj();
            a2 = n1.getj();
            symbol s = new symbol();
            int b;
            s.seta();
            b = s.geta();
            if(b == 1)
                System.out.println(a1 + "+" + "(" +a2 + ")" + "=" + ( a1 + a2 ) );
            if(b == 2)
                System.out.println(a1 + "-" + "(" +a2 + ")" + "=" + (a1 - a2));
            if(b == 3)
                System.out.println(a1 + "*" + "(" +a2 + ")" + "=" + (a1 * a2));
            if(b == 4){
                int c = ((-1) * a1) % ((-1) * a2);
                int c1 = ((-1) * a1 - c) / ((-1) * a2);
                System.out.println(a1 + "/" + "(" +a2 + ")" + "=" + c1 + "余" + c);
            }
        
        }
    }
    public static void fraction() //分数运算
    {
        for(int i = 0 ; i <= 29 ; i++)
        {
            number2 n2 = new number2();
            int b1 , b2 , b3 , b4;
            n2.setnumber();
            b1 = n2.getnumberi();
            b2 = n2.getnumberj();
            b3 = n2.getnumberx();
            b4 = n2.getnumbery();
            symbol s1 = new symbol();
            int c;
            s1.seta();
            c = s1.geta();
            if(c == 1)
                System.out.println(simplify(b2 , b1) + "+" + simplify(b4 , b3) + "=" + simplify(b2 * b3 + b4 * b1 , b1 * b3));
            if(c == 2)
                System.out.println(simplify(b2 , b1) + "-" + simplify(b4 , b3) + "=" + simplify(b2 * b3 - b4 * b1 , b1 * b3));
            if(c == 3)
                System.out.println(simplify(b2 , b1) + "*" + simplify(b4 , b3) + "=" + simplify(b2 * b4 , b1 * b3));
            if(c == 4)
                System.out.println(simplify(b2 , b1) + "/" + simplify(b4 , b3) + "=" + simplify(b2 * b3 , b1 * b4));
        }
    }
    public static void divide() //整除运算
    {
        int i = 0;
        while(i < 30){
            number1 n = new number1();
            int a1 , a2;
            n.seti();
            a1 = n.geti();
            n.setj();
            a2 = n.getj();
            if(a1 % a2 == 0)
            {
                i++;
                System.out.println(a1 + "/" + a2 + "=" + (a1 / a2));
            }
        }    
    }
    public static void divide1() //不整除运算
    {
        int i = 0;
        while(i < 30){
            number1 n = new number1();
            int a1 , a2;
            n.seti();
            a1 = n.geti();
            n.setj();
            a2 = n.getj();
            if(a1 % a2 != 0)
            {
                i++;
                int c = a1 % a2;
                int c1 = (a1 - c) / a2;
                System.out.println(a1 + "/" + a2 + "=" + c1 + "余" + c);
            }
        }
    }
    public static void range() //范围运算
    {
        System.out.println("请输入范围的左边界:");
        Scanner scan1 = new Scanner(System.in);
        int a3;
        a3 = scan1.nextInt();
        System.out.println("请输入范围的右边界:");
        Scanner scan2 = new Scanner(System.in);
        int a4;
        a4 = scan1.nextInt();
        int i = 0;
        while(i < 30){
            number1 n1 = new number1();
            int a1 , a2;
            n1.seti();
            a1 = n1.geti();
            n1.setj();
            a2 = n1.getj();
            symbol s = new symbol();
            int b;
            s.seta();
            b = s.geta();
            if(a1 >= a3 && a1 <= a4 && a2 >= a3 && a2 <= a4)
            {
                i++;
                if(b == 1)
                    System.out.println(a1 + "+" + a2 + "=" + ( a1 + a2 ) );
                if(b == 2)
                    System.out.println(a1 + "-" + a2 + "=" + (a1 - a2));
                if(b == 3)
                    System.out.println(a1 + "*" + a2 + "=" + (a1 * a2));
                if(b == 4){
                    int c = a1 % a2;
                    int c1 = (a1 - c) / a2;
                    System.out.println(a1 + "/" + a2 + "=" + c1 + "余" + c);
                }
            }    
        }
    }
    public static void mixture() //混合运算同时能够控制题目的数量,同时也能检查重复,同时还可以判断出回答的正误
    {
        System.out.println("请输入您想要生成的题目数量");
        Scanner in = new Scanner(System.in);
        int  amount;
        amount = in.nextInt();
        int c = 0;
        int[] list =new int[amount + 1];
        int[] list1 = new int[amount + 1];
        int[] list2 = new int[amount + 1];
        while(c < amount)
        {
            number3 n = new number3();
            n.setnumber();
            int b1 , b2 , b3 , b4;
            b1 = n.getnumberi();
            b2 = n.getnumberj();
            b3 = n.getnumberx();
            b4 = n.getnumbery();
            symbol1 s = new symbol1();
            s.seta();
            symbol1 s1 = new symbol1();
            s1.seta();
            symbol1 s2 = new symbol1();
            s2.seta();
            int a1 , a2 , a3;
            a1 = s.geta();
            a2 = s1.geta();
            a3 = s2.geta();
            if(a1 == 1 && a2 == 1 && a3 == 1)
            {
                if( (b1 + b2 + b3 + b4) >= 0 )
                {
                    System.out.println(b1 + "+" + b2 + "+" + b3 + "+" + b4 +"=");
                    c ++;
                    if(charge( (b1 + b2 + b3 + b4) ) == 1)
                    {
                        list[c] = c ;
                    }
                    else
                    {
                        list1[c] = c ;
                    }
                }
            }
            if(a1 == 1 && a2 == 1 && a3 == 2)
            {
                if((b1 + b2 + b3 - b4) >= 0)
                {
                    System.out.println(b1 + "+" + b2 + "+" + b3 + "-" + b4 +"=" );
                    c ++;
                    if(charge( (b1 + b2 + b3 - b4) ) == 1)
                    {
                        list[c] = c ;
                    }
                    else
                    {
                        list1[c] = c ;
                    }
                }
            }
            if(a1 == 1 && a2 == 2 && a3 == 1)
            {
                if((b1 + b2 - b3 + b4) >= 0)
                {
                    System.out.println(b1 + "+" + b2 + "-" + b3 + "+" + b4 +"=" );
                    c ++;
                    if(charge( (b1 + b2 - b3 + b4) ) == 1)
                    {
                        list[c] = c ;
                    }
                    else
                    {
                        list1[c] = c ; 
                    }
                }
            }
            if(a1 == 1 && a2 == 2 && a3 == 2)
            {
                if((b1 + b2 - b3 - b4) >= 0)
                {
                    System.out.println(b1 + "+" + b2 + "-" + b3 + "-" + b4 +"=" );
                    c ++;
                    if(charge( (b1 + b2 - b3 - b4) ) == 1)
                    {
                        list[c] = c ;
                    }
                    else
                    {
                        list1[c] = c;
                    }
                }
            }
            if(a1 == 2 && a2 == 1 && a3 == 1)
            {
                if((b1 - b2 + b3 + b4) >= 0)
                {
                    System.out.println(b1 + "-" + b2 + "+" + b3 + "+" + b4 +"=" );
                    c ++;
                    if(charge( (b1 - b2 + b3 + b4) ) == 1)
                    {
                        list[c] = c ;
                    }
                    else
                    {
                        list1[c] = c ;
                    }
                }
            }
            if(a1 == 2 && a2 == 1 && a3 == 2)
            {
                if((b1 - b2 + b3 - b4) >= 0)
                {
                    System.out.println(b1 + "-" + b2 + "+" + b3 + "-" + b4 +"=" );
                    c ++;
                    if(charge( (b1 - b2 + b3 - b4) ) == 1)
                    {
                        list[c] = c ;
                    }
                    else
                    {
                        list1[c] = c ;
                    }
                }
            }
            if(a1 == 2 && a2 == 2 && a3 == 1)
            {
                if((b1 - b2 - b3 + b4) >= 0)
                {
                    System.out.println(b1 + "-" + b2 + "-" + b3 + "+" + b4 +"=" );
                    c ++;
                    if(charge( (b1 - b2 - b3 + b4) ) == 1)
                    {
                        list[c] = c ;
                    }
                    else
                    {
                        list1[c] = c ;
                    }
                }
            }
            if(a1 == 2 && a2 == 2 && a3 == 2)
            {
                if((b1 - b2 - b3 - b4) >= 0)
                {
                    System.out.println(b1 + "-" + b2 + "-" + b3 + "-" + b4 +"=" );
                    c ++;
                    if(charge( (b1 - b2 - b3 - b4) ) == 1)
                    {
                        list[c] = c;
                    }
                    else
                    {
                        list1[c] = c ;
                    }
                }
            }
        }
        System.out.println("答对的题目有:");
        for(int d = 0 ; d <= amount ; d ++)
        {
            if(list[d] != 0)
            {
                System.out.println(list[d]);
            }
        }
        System.out.println("答错的题目有:");
        for(int e = 0 ; e <= amount ; e ++)
        {
            if(list1[e] != 0)
            {
                System.out.println(list1[e]);
            }
        }
    }
    
}

技术分享

技术分享

技术分享

                                              技术分享

 

四则运算第三次

标签:divide   判断   bsp   span   system.in   exti   ==   static   next   

原文地址:http://www.cnblogs.com/xhxdeblog/p/6567736.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!