首页 > 代码库 > 四则运算器初步成果小结

四则运算器初步成果小结

目前四则运算程序已经完成了整数之间的加减乘除,随机生成题目,用户输入并判断答案对错。下一步,实现分数之间的出题。fighting。。。。(然而我不会发代码,233333,只能贴图了)

package software;public class fraction {      private int numerator;      private int denominator;      public fraction(int numerator, int denominator){        this.numerator=numerator;        this.denominator=denominator;      }      public double getRet(){        return (double)numerator/denominator;      }      public String toString(){        return numerator+"/"+denominator;      }}

这个类是从网上搞的一个现成的分数类。下面的都是自己的实现过程:

package software;import java.util.Scanner;import java.math.*; public class jisuan {    public char operate(){        int i;        char op = ‘ ‘;        i=(int)(Math.random()*100)%4+1;        switch(i){        case 1:op=‘+‘;break;        case 2:op=‘-‘;break;        case 3:op=‘*‘;break;        case 4:op=‘/‘;        }        return op;        }         public int num(){        int i;        i=(int)(Math.random()*100)+1;        return i;    }        public int maxyueshu( int a, int b ){        return a%b==0?b:maxyueshu(b,a%b);         }        public String calculate(int a,char op,int b){        if(op==‘+‘)            return (a+b)+"";        else if(op==‘-‘)            return (a-b)+"";        else if(op==‘*‘)            return (a*b)+"";        else{            int max=this.maxyueshu(a, b);            a=a/max;            b=b/max;            if(b==1){                return a+"";            }            else{                fraction t=new fraction(a,b);                String tt=t.toString();                return tt;            }        }               }        public void intmatch(){        int a=this.num();        int b=this.num();        char op=this.operate();        int answer;        String myanswer;        System.out.print("请做题:"+a+op+b+"=");        String answer1=this.calculate(a, op, b);        //System.out.print(answer1);        Scanner in= new Scanner(System.in);         myanswer=in.next();        if(myanswer.equals(answer1)){            System.out.print("恭喜,答案正确。");        }        else{            System.out.print("很抱歉,答案错误。正确答案为:"+answer1);        }    }    public void frcmatch(){            }    public static void main(String[] args) {        jisuan a=new jisuan();        a.intmatch();        // TODO Auto-generated method stub    }}

 

四则运算器初步成果小结