Java第二次考试

Posted 暗梦

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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();
        }
    }

}

于下午四点四十完成

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

以上是关于Java第二次考试的主要内容,如果未能解决你的问题,请参考以下文章

第二次在对话框中膨胀片段时出错

java wicket链接onclick代码在第二次点击时不执行

使用选项卡第二次返回片段显示空白片段

第二次考试 试题

自考的第二次考试

20165315 第二次考试课下补做