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

四则运算

时间:2017-09-24 11:35:43      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:return   函数   乘法   完成   count   system   class   result   随机   

非定义类的方式完成四则运算,最大值最小值。(为计算方便用十位数字进行测试)

import java.util.*;
public class mathcaculate {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        play();
        
    }
    public static void play()                      //界面函数
    {
        float result=0.0f;char ch=‘0‘;    
        Scanner in=new Scanner(System.in);
        while(true)
        {
            System.out.println("***开始答题系统***");
            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...最小值问题***");
            int m=in.nextInt();
            switch(m)
            {
            case 1:
                ch=inchartest();        
                break;
            case 2:
                ch=‘+‘;
                break;
            case 3:
                ch=‘-‘;
                break;
            case 4:
                ch=‘*‘;
                break;
            case 5:
                ch=‘/‘;
                break;
            case 6:
                ch=‘a‘;
                break;
            case 7:
                ch=‘i‘;
                break;
                
            }
            int count=0,k=0;
            while(true)
            {
                result=paper(ch);
                int io=-1;        
                for(int i=0;i<5;i++)
                {
                    io=in.nextInt();
                    if(io==-1)
                        break;
                    if(io==result)
                    {
                        System.out.println("回答正确!");count++;
                        break;
                    }
                    else
                        System.out.println("剩余次数"+(5-i));
                        if(i==4) System.out.println("正确结果"+result);            
                }
                k++;System.out.println("已答"+k+"    正确"+count);
                System.out.println("-1...结束,继续!");
                if(io==-1)
                    break;
            }
            
        }    
    }
    private static float paper(char ch) {              出题系统
        // TODO Auto-generated method stub
        int left,right;float result;int array[]=new int[5];
        if(ch!=‘a‘&&ch!=‘i‘)
        {                
        left=innumtest();right=innumtest();
        result=caculate(left,right,ch);
        System.out.print(left);
        System.out.print(ch);
        System.out.print(right);
        System.out.println("=");
        }
    else
    {
        for(int j=0;j<5;j++)
            array[j]=innumtest();
        for(int j=0;j<array.length;j++)
            System.out.print(array[j]+" ");
        result=caculate(array,ch);
    }
        return result;
        
    }
    public static float caculate(int left,int right,char ch)//结果计算
    {
        float result=0.0f;
        switch(ch)
        {
        case ‘+‘:
            result=left+right;
            break;
        case ‘-‘:
            result=left-right;
            break;
        case ‘*‘:
            result=left*right;
            break;
        case ‘/‘:
            result=(float) (1.0*left/right);
            break;            
        }
        return result;
        
    }
    public static float caculate(int array[],char ch)
    {
        float result=0.0f;
        switch(ch)
        {
        case ‘a‘:
            System.out.println("求最大值? ");            
            result=max(array);
            break;
        case ‘i‘:
            System.out.println("求最小值? ");
            result=min(array);
            break;
            
        }
        return result;
        
    }
    public static char inchartest()//随机运算函数
    {
        Random random=new Random();
        int test=random.nextInt(6);
        char ch=‘0‘;
        switch(test)
        {
        case 0:ch= ‘+‘;    break;        
        case 1:ch= ‘-‘;    break;            
        case 2:ch= ‘*‘;    break;        
        case 3:ch= ‘/‘;    break;            
        case 4:ch= ‘a‘;    break;        
        case 5:ch= ‘i‘;    break;        
            
        }
        return ch;
        
    }
    public static int max(int array[])
    {
        int temp=array[0];
        for(int i=0;i<array.length;i++)
            if(temp<array[i])
                temp=array[i];
        return temp;
    }
    public static int min(int array[])
    {
        int temp=array[0];
        for(int i=0;i<array.length;i++)
            if(temp>array[i])
                temp=array[i];
        return temp;
    }
    
    public  static int innumtest()
    {
        int number;
        number=rand_number();
        return number;
    }
    public static int rand_number() {
        // TODO Auto-generated method stub
        Random random=new Random();
        return(random.nextInt(100));
    }

}

 

四则运算

标签:return   函数   乘法   完成   count   system   class   result   随机   

原文地址:http://www.cnblogs.com/wgx521/p/7586588.html

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