• 四则运算


    package 一百;
    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.OutputStreamWriter;
    import java.util.Scanner;
    
    public class Build {     
        @SuppressWarnings("resource")
        public static void main (String[] args)throws IOException{
            File file = new File("D:/java/eclipse/100道题/Test.txt"); 
            if(!file.getParentFile().exists()){ //如果文件的目录不存在
                file.getParentFile().mkdirs(); //创建目录           
            }
            FileOutputStream fis=new FileOutputStream(file);
            OutputStreamWriter output = new OutputStreamWriter(fis);
            int[] answer=new int[100];
            for (int i = 0; i < 100; i++) {
                int t = 0;
                String strz = "";   
                int x=(int)(Math.random()*100)+1;
                int y=(int)(Math.random()*100)+1;
                int z=(int)(Math.random()*100)+1;
                if( z<=15 ) {
                    strz = "+";
                    if ((x+y) > 100) {//相加结果小于等于100        
                        x = x / 2;
                        y = y / 2;
                    }
                    answer[i]=x+y;
                }
                if( z>15 && z<=30 ) {
                    strz = "-";
                    if( x < y ){//减数大于被减数
                        t = y;
                        y = x;
                        x = t;
                    }
                    answer[i]=x-y;
                }
                if( z>30 && z<=45) {//两个个位数相乘
                    strz = "×";
                    x = x % 10;
                    y = y % 10;
                    answer[i]=x*y;
                }
                if( z>45 ) {
                    strz = "÷";
                    y = ( y % 10 ) + 1;//除数不能为0
                    if( x % y !=0){//余数不为0
                        strz = "-";
                        if( x < y ){//减数大于被减数
                            t = y;
                            y = x;
                            x = t;
                        }
                        answer[i]=x-y;
                    }
                    else{
                        answer[i]=x/y;
                    }
                }
                String strx = String.valueOf( x );
                String stry = String.valueOf( y );      
                String ques = strx + strz + stry + "=";
                output.write(ques+"
    ");
            }
            output.close();
            File file1=new File("D:/java/eclipse/100道题/Test.txt");
            int i=0;
            int score=0;
            if(file1.exists()){
                try{
                    FileReader fileReader = new FileReader(file1);  
                    BufferedReader br = new BufferedReader(fileReader);  
                    String lineContent = null;  
                    while((lineContent = br.readLine())!=null){  
                        System.out.println(lineContent); 
                        Scanner input = new Scanner(System.in);
                        int a=input.nextInt();
                        if(a==answer[i]) {
                            System.out.println("回答正确!");
                            score++;
                        }
                        else
                        {
                            System.out.println("回答错误! 答案是"+answer[i]);
                        }
                        i++;
                    }  
                    br.close();  
                    fileReader.close();  
                }
                catch (FileNotFoundException e1) {  
                    System.out.println("no this file");//文件不存在抛异常  
                    e1.printStackTrace();  
                }
                catch (IOException e1) {  
                    System.out.println("io exception");  
                    e1.printStackTrace();  
                } 
            }
            System.out.println("分数为:"+score);
        }
    }

    用了将近四节课,主要是核对答案时出现错误,数组引用不出来。这次测试让我发现许多自己知识的盲点,以后还会更加努力的。

  • 相关阅读:
    创建型模式
    react-React深入-一等公民-props-onChange
    [react] React 新手必须知道的 N 件事
    [react] 细数 React 的原罪
    创业一年,苟且偷生
    好书推荐-《人类简史》.md—/Users/zjh/Documents/我的文章/好书推荐-《人类简史》
    社会科学-主题阅读.md—/Users/zjh/Documents/我的文章
    好书推荐-《国富论》-15-09.md—/Users/zjh/Documents
    readme.md—/Users/zjh/Documents/我的文章/[PHP]swoole_server几个进程的分工
    [PHP]Yii2框架的坑.md—/Users/zjh/Documents/我的文章/[PHP]Yii2框架的坑
  • 原文地址:https://www.cnblogs.com/yuanxiaochou/p/9966778.html
Copyright © 2020-2023  润新知