1. 程式人生 > >四則運算隨機生成100題

四則運算隨機生成100題

package demo;
   
   import java.io.BufferedReader;
   import java.io.BufferedWriter;
   import java.io.FileReader;
   import java.io.FileWriter;
   import java.io.IOException;
   import java.util.Scanner;
  
  public class sizeyunsuan {
  
      static String[] question = new String[100];
      static
int[] answer = new int[100]; public static int getRandom(int n, int m) { return (int) (Math.random() * (m - n) + n); } public static char getCharRandom() { char sign = 0; int Sn; Sn = getRandom(1, 5); switch (Sn) { case
1: sign = '+'; break; case 2: sign = '-'; break; case 3: sign = '×'; break; case 4: sign = '÷'; break; } return sign; } public
static void main(String[] args) { // TODO Auto-generated method stub @SuppressWarnings("resource") Scanner cin = new Scanner(System.in); //File file = new File("E:\\szys.txt"); int i = 0; int huida,score = 0; do { int x = (int) (Math.random() * (100 - 1 )+ 1); //產生1-100的隨機數 int y = (int) (Math.random() * (100 - 1 )+ 1); //產生1-100的隨機數 char sign = getCharRandom(); switch(sign) { case '+': question[i] = x +""+ sign +""+ y + "="; huida = x + y; answer[i] = huida; //System.out.println( "("+ (i+1) +")"+ x + " " + sign + " " + y + "=" ); i++;break; case '-': if(x < y) //判斷減數與被減數的大小關係 { int temp; temp = x; x = y; y = temp; } question[i] = x +""+ sign +""+ y + "="; huida = x - y; answer[i] = huida; //System.out.println( "("+ (i+1) +")"+ x + " " + sign + " " + y + "=" ); i++;break; case '×': { x = (int) (Math.random() * (10 - 1 )+ 1);//新生成x,y<9的隨機數 y = (int) (Math.random() * (10 - 1 )+ 1); question[i] = x +""+ sign +""+ y + "="; huida = x * y; answer[i] = huida; //System.out.println( "("+ (i+1) +")"+ x + " " + sign + " " + y + "=" ); i++; };break; case '÷': do //迴圈生成除法 { y = (int) (Math.random() * (10 - 1 )+ 1); x = (int) (Math.random() * (9*y - 1 )+ 1); } while(x % y != 0) ; question[i] = x +""+ sign+"" + y + "="; huida = x / y; answer[i] = huida; //System.out.println( "("+ (i+1) +")"+ x + " " + sign + " " + y + "=" ); i++;break; } } while(i<10); try { @SuppressWarnings("resource") BufferedWriter br= new BufferedWriter(new FileWriter("sizeyunsuan.txt")); for(int k = 0;k<10;k++) { br.write("("+(k+1)+")"+String.valueOf(question[k])+"*"+String.valueOf(answer[k]));//讀取陣列進緩衝區 br.newLine();//寫入新的一行,換行 br.flush();//將緩衝區存入txt } } catch (IOException e) { //檔案讀寫異常 e.printStackTrace(); } try { String line = null; @SuppressWarnings("resource") BufferedReader re = new BufferedReader(new FileReader("sizeyunsuan.txt"));//新定義 while((line = re.readLine())!= null) {//逐行讀取檔案 String [] txt = line.split("\\*");//以*為分界,將.txt分開成問題和答案兩塊 System.out.println(txt[0]);//輸出題目 System.out.print("Your answer:"); String an = cin.next(); if(txt[1].equals(an))//判斷答案與回答 { System.out.println("回答正確!"); score++; } else System.out.println("回答錯誤!正確答案:" + txt[1]); } System.out.println("共答對"+ score + "題,答錯" + (10-score) + "題"); }catch(IOException e) { e.printStackTrace(); } } }

我在這次試驗卡了很長時間,由於檔案的應用沒有掌握太牢固以及一些知識點的應用有偏差;

使用BufferedWriter寫入文字時不用將文字轉換成位元組陣列,直接整行整行的寫入,大大提供了寫入效率。