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 }elsestatistics = 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();}}}于下午四點四十完成
總結:主要自己對于緩沖流導入文本文件夾,輸出文本文件夾,不太理解,一直困于此處,對于四則運算的正誤判斷也有些許的疑問,然后通過百度查閱,以及詢問同學弄懂了自己不是很會的地方,感覺自己在這次課堂測驗上學到了很多
轉載于:https://www.cnblogs.com/love-nan/p/9964919.html
總結
- 上一篇: Jetty:配置概览-怎么配置Jetty
- 下一篇: HTML5-基础语法