• Java四则运算V3.0


    第三版基本完成了

    题目:

    课堂测试3: 2、可定制(数量/打印方式):输入大的数量值,测试一下系统是否崩溃,反向查找系统是否优化的余地; 3、定制操作数的个数: 4、定制是否有乘除法 5、定制是否有括号(随机加入) 6 、定制数值范围(确定操作数的取值范围)

    package demo;
    import java.util.Scanner;
    
    public class chuti {
    
        static String[] question = new String[100];
        static int[] answer = new int[100];
    
        public static int getRandom(int n, int m) {
            // 产生n->m的随机数
            return (int) (Math.random() * (m - n) + n);
        }
    
        public static char getCharRandom() {
            // 随机产生四种运算符
            char sign = 0;
            int Sn;
            Sn = getRandom(1, 5);
            switch (Sn) {
            case 1:
                sign = '+';
                break;
            case 2:
                sign = '-';
                break;
            case 3:
                sign = '×';
                break;
            case 4:
                sign = '÷';
                break;
            }
            return sign;
        }
    
        public static  void main(String[] args) {
            // TODO Auto-generated method stub
            Scanner cin = new Scanner(System.in);
            //File file = new File("E:\\szys.txt");
            int i = 0;
            int huida,score = 0;
             do
             {
                 int  x = (int) (Math.random() * (100 - 1 )+ 1);  //产生1-100的随机数
                 int  y = (int) (Math.random() * (100 - 1 )+ 1);  //产生1-100的随机数
                 char sign = getCharRandom();
                 /*
                  * 判断乘法的范围*/
                 switch(sign)
                 {
                 case '+':
                     question[i] = x +""+ sign +""+ y + "=";
                     huida = x + y;
                     answer[i] = huida;
                     //System.out.println( "("+ (i+1) +")"+ x + " " + sign + " " + y + "="     );
                     i++;break;
                 case '-':
                     if(x < y)                        //判断减数与被减数的大小关系
                     {
                         int temp;
                         temp = x;
                         x = y;
                         y = temp;
                     }
                     question[i] = x +""+ sign +""+ y + "=";
                     huida = x - y;
                     answer[i] = huida;
                     //System.out.println( "("+ (i+1) +")"+ x + " " + sign + " " + y + "="     );
                     i++;break;
                 case '×':
                 {
                     x = (int) (Math.random() * (10 - 1 )+ 1);//新生成x,y<9的随机数
                     y = (int) (Math.random() * (10 - 1 )+ 1);
                     question[i] = x +""+ sign +""+ y + "=";
                     huida = x * y;
                     answer[i] = huida;
                         //System.out.println( "("+ (i+1) +")"+ x + " " + sign + " " + y + "="     );
                     i++;
                 };break;
                 case '÷':
                     do                                             //循环生成除法
                     {
                         y = (int) (Math.random() * (10 - 1 )+ 1);
                         x = (int) (Math.random() * (9*y - 1 )+ 1);
    
                     }
                    while(x % y != 0) ;
                    question[i] = x +""+ sign+"" + y + "=";
                    huida = x / y;
                     answer[i] = huida;
                    //System.out.println( "("+ (i+1) +")"+ x + " " + sign + " " + y + "="     );
                    i++;break;
                     }
            }
            while(i<10);
    
    
        try {
                BufferedWriter br= new BufferedWriter(new FileWriter("szys.txt"));
                for(int k = 0;k<10;k++)
                {
                    br.write("("+(k+1)+")"+String.valueOf(question[k])+"*"+String.valueOf(answer[k]));//读取数组进缓冲区
                    br.newLine();//写入新的一行,换行
                    br.flush();//将缓冲区存入txt
                }
            } catch (IOException e) {
            //文件读写异常
            e.printStackTrace();
        }
    
        try {
                String line = null;
                BufferedReader re = new BufferedReader(new FileReader("szys.txt"));//新定义
                while((line = re.readLine())!= null)
                {//逐行读取文件
                    String [] txt = line.split("\\*");//以*为分界,将.txt分开成问题和答案两块
                    System.out.println(txt[0]);//输出题目
                    System.out.print("Your answer:");
                    String an = cin.next();
                    if(txt[1].equals(an))//判断答案与回答
                    {
                        System.out.println("回答正确!");
                        score++;
                    }
                    else
                        System.out.println("回答错误!正确答案:" + txt[1]);
                }
                System.out.println("共答对"+ score + "题,答错" + (10-score) + "题");
        }catch(IOException e)
            {
                e.printStackTrace();
            }
    
    }
    }
  • 相关阅读:
    【JavaScript】7-27 冒泡法排序 (20分)
    【JavaScript】7-26 单词长度 (15分)
    【JavaScript】7-25 念数字 (15分)
    【JavaScript】7-24 约分最简分式 (15分)
    自适应单本小说网站源码,基于bootstrap+dedecms。
    草根站长建站需要掌握或者了解的5种技术
    5个国内优秀网站设计案例分享
    (转)常用RGB色值表
    小波变换和motion信号处理(三)(转)
    小波变换和motion信号处理(二)(转)
  • 原文地址:https://www.cnblogs.com/zyljal/p/14156880.html
Copyright © 2020-2023  润新知