1. 程式人生 > >四則運算(檔案儲存)

四則運算(檔案儲存)

當堂測試,將四則運算隨機生成100道題,儲存在TXT文字中,同時在題目後面儲存*作為分界符,實現答題判斷對錯並彙總功能。

【設計思想】

  1.隨機生成100道四則運算計算題

  2.將生成的100道題儲存在.txt檔案裡

  3.讀取txt檔案,並作答,判斷答案正誤,輸出彙總

  設計思想很簡單,一些程式碼實現也不是很難,真正困擾我程式設計的是檔案的輸入流和輸出流操作,BufferedReader和BufferedWriter,對於檔案的操作差的太多,自己也不知道如何下手檔案問題,最後還是在同學幫助下才弄懂一點點操作。空餘時間還是應該多練一下。

【原始碼】為測試方便,將生成題目數改成10道

  1
package demo; 2 3 import java.io.BufferedReader; 4 import java.io.BufferedWriter; 5 import java.io.FileReader; 6 import java.io.FileWriter; 7 import java.io.IOException; 8 import java.util.Scanner; 9 10 public class chuti { 11 12 static String[] question = new String[100]; 13
static int[] answer = new int[100]; 14 15 public static int getRandom(int n, int m) { 16 // 產生n->m的隨機數 17 return (int) (Math.random() * (m - n) + n); 18 } 19 20 public static char getCharRandom() { 21 // 隨機產生四種運算子 22 char sign = 0; 23 int
Sn; 24 Sn = getRandom(1, 5); 25 switch (Sn) { 26 case 1: 27 sign = '+'; 28 break; 29 case 2: 30 sign = '-'; 31 break; 32 case 3: 33 sign = '×'; 34 break; 35 case 4: 36 sign = '÷'; 37 break; 38 } 39 return sign; 40 } 41 42 public static void main(String[] args) { 43 // TODO Auto-generated method stub 44 Scanner cin = new Scanner(System.in); 45 //File file = new File("E:\\szys.txt"); 46 int i = 0; 47 int huida,score = 0; 48 do 49 { 50 int x = (int) (Math.random() * (100 - 1 )+ 1); //產生1-100的隨機數 51 int y = (int) (Math.random() * (100 - 1 )+ 1); //產生1-100的隨機數 52 char sign = getCharRandom(); 53 /* 54 * 判斷乘法的範圍*/ 55 switch(sign) 56 { 57 case '+': 58 question[i] = x +""+ sign +""+ y + "="; 59 huida = x + y; 60 answer[i] = huida; 61 //System.out.println( "("+ (i+1) +")"+ x + " " + sign + " " + y + "=" ); 62 i++;break; 63 case '-': 64 if(x < y) //判斷減數與被減數的大小關係 65 { 66 int temp; 67 temp = x; 68 x = y; 69 y = temp; 70 } 71 question[i] = x +""+ sign +""+ y + "="; 72 huida = x - y; 73 answer[i] = huida; 74 //System.out.println( "("+ (i+1) +")"+ x + " " + sign + " " + y + "=" ); 75 i++;break; 76 case '×': 77 { 78 x = (int) (Math.random() * (10 - 1 )+ 1);//新生成x,y<9的隨機數 79 y = (int) (Math.random() * (10 - 1 )+ 1); 80 question[i] = x +""+ sign +""+ y + "="; 81 huida = x * y; 82 answer[i] = huida; 83 //System.out.println( "("+ (i+1) +")"+ x + " " + sign + " " + y + "=" ); 84 i++; 85 };break; 86 case '÷': 87 do //迴圈生成除法 88 { 89 y = (int) (Math.random() * (10 - 1 )+ 1); 90 x = (int) (Math.random() * (9*y - 1 )+ 1); 91 92 } 93 while(x % y != 0) ; 94 question[i] = x +""+ sign+"" + y + "="; 95 huida = x / y; 96 answer[i] = huida; 97 //System.out.println( "("+ (i+1) +")"+ x + " " + sign + " " + y + "=" ); 98 i++;break; 99 } 100 } 101 while(i<10); 102 103 104 try { 105 BufferedWriter br= new BufferedWriter(new FileWriter("szys.txt")); 106 for(int k = 0;k<10;k++) 107 { 108 br.write("("+(k+1)+")"+String.valueOf(question[k])+"*"+String.valueOf(answer[k]));//讀取陣列進緩衝區 109 br.newLine();//寫入新的一行,換行 110 br.flush();//將緩衝區存入txt 111 } 112 } catch (IOException e) { 113 //檔案讀寫異常 114 e.printStackTrace(); 115 } 116 117 try { 118 String line = null; 119 BufferedReader re = new BufferedReader(new FileReader("szys.txt"));//新定義 120 while((line = re.readLine())!= null) 121 {//逐行讀取檔案 122 String [] txt = line.split("\\*");//以*為分界,將.txt分開成問題和答案兩塊 123 System.out.println(txt[0]);//輸出題目 124 System.out.print("Your answer:"); 125 String an = cin.next(); 126 if(txt[1].equals(an))//判斷答案與回答 127 { 128 System.out.println("回答正確!"); 129 score++; 130 } 131 else 132 System.out.println("回答錯誤!正確答案:" + txt[1]); 133 } 134 System.out.println("共答對"+ score + "題,答錯" + (10-score) + "題"); 135 }catch(IOException e) 136 { 137 e.printStackTrace(); 138 } 139 140 } 141 }

【執行結果】

【szys.txt】