zoukankan      html  css  js  c++  java
  • Java第二次考试

    代码

    package sizeyunsuan;
    import java.io.FileNotFoundException;
    import java.io.PrintStream;
    import java.util.Random;
    import java.util.Scanner;
    public class SiZe {
        @SuppressWarnings("unused")
          public static void main(String[] args) throws FileNotFoundException {
              String[] operate = new String[] { "+", "-", "*", "/" };//定义运算符数组
              int[] numbers = new int[100];//定义数字部分数组
              for (int i = 1; i <= 100; i++) {
                 numbers[i - 1] = i;
              }
             Random rand = new Random();//定义随机数
             @SuppressWarnings("resource")
             Scanner in = new Scanner( System.in );
             String fiilename ="d:/shuchu.txt";
             int operand1 = 0 ;
             int operand2 = 0;
             int operator;
             int result;
             int count=0;                                //统计正确的题目数量
             int i;
             duqu[] cal = new duqu[100];    //30道题目
             
             //随机题目并显示序号与题目
             for( i=0; i<100; i++ ){
                 operator = rand.nextInt(4)+1;
                 
                 switch( operator ){
                     case 1:
                         operand1 = rand.nextInt(100);
                         operand2 = rand.nextInt(100);
                         break;
                     case 2:                                //减法不出现负数
                         operand1 = rand.nextInt(100);
                         operand2 = rand.nextInt( operand1 );
                         break;
                     case 3:                                //乘除范围更小
                         operand1 = rand.nextInt(10);
                         operand2 = rand.nextInt(10);
                         break;
                     case 4:
                         operand2 = rand.nextInt(9)+1;
                         operand1 = rand.nextInt(81-operand2)+operand2;
                         break;
                 }
                 
                 System.out.print( (i+1) +"  ");
                 cal[i] = new duqu( operand1, operator, operand2);
                 cal[i].manager();
                 if(i==0)
                     duqu.flushFile(fiilename);
                 duqu.OutputFile(fiilename,cal[i]);
                 PrintStream out=System.out;
             
                 
                
         }
        }
    }
    package sizeyunsuan;
    
    import java.io.*;
    public class duqu {
        
        private int operator;            //操作数
        private int operand1;            //操作数前面的数
        private int operand2;            //操作数后面的书
        private int result;                //运算结果,除法结果取整数部分
        private int statistics;            //统计正误
        private String str;                //记录题目
        
        public int getResult(){
            return result;
        }
        
        public int getStatistics(){        
            return statistics;
        }
        
        public duqu( int operand1 , int operator , int operand2 ){
            this.operand1 = operand1;
            this.operator = operator;
            this.operand2 = operand2;
        }
        
        public void manager(){
            switch(operator){
                case 1:
                    str = operand1 +" + " + operand2 +" = ";
                    result = operand1 + operand2;
                    break;
                case 2:
                    str = operand1 +" - " + operand2 +" = ";
                    result = operand1 - operand2;
                    break;
                case 3:
                    str = operand1 +" * " + operand2 +" = ";
                    result = operand1 * operand2;
                    break;
                case 4:
                    str = operand1 +" / " + operand2 +" = ";
                    result = operand1 / operand2;
                    break;
            }
            System.out.println(str);
        }
        
        public void judge( int result ){        //判断正误
            if( this.result == result){
                statistics = 1;                    //正确为1
            }
            else
                statistics = 0;                    //错误为0
        }
        
        public static void OutputFile(String filename,duqu cal){
            @SuppressWarnings("unused")
            File file = new File(filename);
            FileWriter fw = null;
            try {
                fw = new FileWriter(filename,true);
            } catch (IOException e) {
                e.printStackTrace();
            }
            PrintWriter p = new PrintWriter(fw);
            p.println(cal.str);
            p.close();
            try {
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        
        public static void flushFile(String filename){
            File file = new File(filename);
            FileWriter fw;
            try {
                fw = new FileWriter(file);
                fw.write("");
                fw.flush();
                fw.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    
    }

    于下午四点四十完成

    总结:主要自己对于缓冲流导入文本文件夹,输出文本文件夹,不太理解,一直困于此处,对于四则运算的正误判断也有些许的疑问,然后通过百度查阅,以及询问同学弄懂了自己不是很会的地方,感觉自己在这次课堂测验上学到了很多

  • 相关阅读:
    mysql 双主高可用配置
    lsyncd实时同步搭建指南
    tomcat优化
    nginx + tomcat + https配置
    supervisor安装文档
    移动设备的分辨率
    Python零基础入门(13)-------语句与流程控制
    Python零基础入门(12)-------文件读写
    Python零基础入门(11)-------dict 字典表
    Python零基础入门(10)------- str 字符串
  • 原文地址:https://www.cnblogs.com/love-nan/p/9964919.html
Copyright © 2011-2022 走看看