1. 程式人生 > >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();
        }
    }

}

於下午四點四十完成

總結:主要自己對於緩衝流匯入文字資料夾,輸出文字資料夾,不太理解,一直困於此處,對於四則運算的正誤判斷也有些許的疑問,然後通過百度查閱,以及詢問同學弄懂了自己不是很會的地方,感覺自己在這次課堂測驗上學到了很多