zoukankan      html  css  js  c++  java
  • 简单的算术运算

    用线性同余法生成随机数序列的公式:r1=(multiplier*r0+increment)%modulus   序列中的每一个数r1,可以由它的前一个数r0计算出来。例如,如果有 :r1=(79253*r0+24897)%65536,可以产生65536个不相同的整型随机数。本实践项目设计一个方法作为随机数生成器,生成一位、两位或三位的随机数。利用这个随机数生成器,编写一个简单算术的练习程序。 

    package exam1;
    import java.io.*;
    
    public class ExerciseSystem {
        int a=0,b=0;
        int diffcon=0;
        char op='+';
        
        public int rand(int r){
            return(13285*r+24897)%65536;
        }
        
        public int InputIntData()throws IOException{
            byte buf[]=new byte[20];
            String str;
            System.in.read(buf);
            str=new String(buf);
            return Integer.parseInt(str.trim());
        }
        
        private void GetOPDatas(){
            try{
                a=rand(b);
                b=rand(a);
            }catch(Exception e){}
            switch(diffcon){
            case 1:a%=9; b%=9; break;
            case 2:a%=99; b%=99; break;
            case 3:a%=999; b%=999; break;
            }
            if(a<b){
                int t=a;  a=b;  b=t;
            }
        }
        
        private char InputOpr()throws IOException{
            return (char)System.in.read();
        }
        
        public void menul1(){
            do{
                System.out.println("  选择难度:");
                System.out.println("   一位数运算------------1:");
                System.out.println("   两位数运算------------2:");
                System.out.println("   三位数运算------------3:");
                System.out.println("   不做了,退出-----------0:");
                try{
                    diffcon=InputIntData();
                }catch(IOException e){
                    System.out.println("输入难度错误,系统退出!");
                }
            }while(diffcon!=0&&diffcon!=1&&diffcon!=2&&diffcon!=3);
        }
    
       public void selectOpr()throws IOException{
        do{
            System.out.println("  选择运算符并给出答案");
            System.out.println("    加法---------+");
            System.out.println("    减法---------——");
            System.out.println("    乘法---------*");
            System.out.println("    除法---------/");
            try{
                op=InputOpr();
            }catch(IOException e){
                System.out.println("输入运算符错误");
            }
        }while(op!='+'&&op!='-'&&op!='*'&&op!='/');
        return;
       }
       
       public int calculate(){
           switch(op){
            case'+':return a+b;
            case'-':return a-b;
            case'*':return a*b;
            case'/':
                if(b!=0)  return a/b;
                else{
                    b=1;
                    System.out.println(a+"/"+b+"="+a);
                    return a;
                }
            default:return -2;
           }
       }
       
       public static void main(String args[])throws IOException{
           ExerciseSystem es=new ExerciseSystem();
           int answer=0;
           int right=0,error=0,total=-1;
           while(true){
               total++;
               es.menul1();
               es.GetOPDatas();
               if(es.diffcon==0){
                   System.out.println("共做"+total+"道题!");
                   System.out.println("做对"+right+"道题!");
                   System.out.println("做错"+error+"道题!");
                   System.out.println("分数"+(100.0*right/total));
                   return;
               }
               System.out.println("两个操作数为:"+es.a+"	"+es.b);
               try{
                   es.selectOpr();
               }catch(IOException e){}
               try{
                   answer=es.InputIntData();
               }catch(IOException e){
                   System.out.println("数据输入错误!!");
               }catch(java.lang.NumberFormatException e){
                   System.out.println("输入的数据格式错误!!!");
               }
               System.out.println(""+es.a+(char)es.op+es.b+"="+answer);
               if(answer==es.calculate()){
                   right++;
                   switch(right%5){
                   case 0:System.out.println("恭喜你做对了!");  break;
                   case 1:System.out.println("太好了,就这样做!");  break;
                   case 2:System.out.println("你真棒!做对了!");  break;
                   case 3:System.out.println("你做的很好,又对了!");  break;
                   case 4:System.out.println("Right!^-^");  break;
                   }
                   continue;
               }
               else{
                   error++;
                   switch(error%3){
                   case 0:System.out.println("仔细想想!");   break;
                   case 1:System.out.println("不要马虎!");   break;
                   case 2:System.out.println("加油!总会成功的!");   break;
                   }
               }
               while(answer!=es.calculate()){
                   System.out.println("计算错误。重做!");
                   System.out.println(""+es.a+(char)es.op+es.b+"=");
               }try{
                   answer=es.InputIntData();
               }catch(IOException e){}
           }
           
       }
    }

     

  • 相关阅读:
    代码守恒定律
    第一个Dockerfile
    服务器项目白名单设置
    TOMCAT禁用不安全请求方式
    标准单例模式
    二进制,八进制,十进制,十六进制!!!!
    JAVA按层级遍历二叉树
    String的+
    安装 Rational Rose 启动报错:无法启动此程序,因为计算机中丢失 suite objects.dll
    java中文乱码问题
  • 原文地址:https://www.cnblogs.com/ljs-666/p/7789566.html
Copyright © 2011-2022 走看看