1. 程式人生 > >第九周課堂測試

第九周課堂測試

原始碼:

package 檔案讀寫;//16:13
import java.io.*;
import java.util.Random;
import java.util.Scanner;
public class yunsuan
{  static  Scanner sc=new Scanner(System.in);
static int[] c=new int[100];
static int m=0;
static int index1=0;
static int index2=0;
public static void main(String[] args)throws IOException {
 

   try {
          BufferedWriter out = new BufferedWriter(new FileWriter("D:/ps/Test.txt"));
            for(int n=0;n<100;n++)
            {
             int i=(int)(1+Math.random()*100);
                int j=(int)(1+Math.random()*100);
                int k=(int)(1+Math.random()*100);
                Random r=new Random();
                int a=r.nextInt(4);
                int b=r.nextInt(4);
            if(a==0)
            {
             if(b==0)
             {out.write(i+"+"+j+"+"+k+"=");
             out.newLine();
             out.write("*");
             out.newLine();
               c[n]=i+j+k;
           }
               if(b==1)
              {out.write(i+"+"+j+"-"+k+"="); 
              out.newLine();
              out.write("*");
              out.newLine();
              c[n]=i+j-k;
        
              }
                       if(b==2)
              {out.write(i+"+"+j+"*"+k+"=");
                      out.newLine();
                      out.write("*");
                      out.newLine();
                    c[n]=i+j*k;
                    }
             if(b==3)
            
              {out.write(i+"+"+j+"/"+k+"=");
             out.newLine();
             out.write("*");
             out.newLine();
             c[n]=i+j/k;
     
             }
             }
            if(a==1)
            {  
             if(b==0)
             {out.write(i+"-"+j+"+"+k+"=");
             out.newLine();
             out.write("*");
             out.newLine();
         c[n]=i-j+k;
        
             }
               if(b==1)
               {
              out.write(i+"-"+j+"-"+k+"=");
              out.newLine();
              out.write("*");
              out.newLine();
           c[n]=i-j-k;
     
            }
             if(b==2) {
              out.write(i+"-"+j+"*"+k+"=");
              out.newLine();
              out.write("*");
              out.newLine();
           c[n]=i-j*k;
      
            }
             if(b==3)
             {
              out.write(i+"-"+j+"/"+k+"=");
              out.newLine();
              out.write("*");
              out.newLine();
              c[n]=i-j/k;
     
             }
             }
            if(a==2)
            {   if(b==0)
             {out.write(i+"*"+j+"+"+k+"=");
             out.newLine();
             out.write("*");
             out.newLine();
           c[n]=i*j+k;
       
             }
               if(b==1)
               {
              out.write(i+"*"+j+"-"+k+"=");
              out.newLine();
              out.write("*");
              out.newLine();
          c[n]=i*j-k;
        
            }
             if(b==2) {
              out.write(i+"*"+j+"*"+k+"=");
              out.newLine();
              out.write("*");
              out.newLine();
         c[n]=i*j*k;
          
            }
             if(b==3)
             {
              out.write(i+"*"+j+"/"+k+"=");
              out.newLine();
                out.write("*");
                out.newLine();
              c[n]=i*j/k;
              
  
             }          
             }
            if(a==3)
            {  
             if(b==0)
             {out.write(i+"/"+j+"+"+k+"=");
             out.newLine();
             out.write("*");
             out.newLine();
         c[n]=i/j+k;
        
             }
               if(b==1)
               {
              out.write(i+"/"+j+"-"+k+"=");
              out.newLine();
              out.write("*");
              out.newLine();
        c[n]=i/j-k;
         
            }
             if(b==2)
             {
              out.write(i+"/"+j+"*"+k+"=");
              out.newLine();
              out.write("*");
              out.newLine();
         c[n]=i/j*k;
          
            }
             if(b==3)
             {
              out.write(i+"/"+j+"/"+k+"=");
              out.newLine();
              out.write("*");
              out.newLine();
         c[n]=i/j/k;
       
             }
             }
            }
         

              out.close();
             System.out.println("檔案建立成功");
             FileReader fr=new FileReader("D:/ps/Test.txt");
       BufferedReader br=new BufferedReader(fr);
       String line=br.readLine();
       while(line!=null)
       { 
       if(line.length()!=1)
       {
        System.out.println(line);
        int x=sc.nextInt();
        System.out.println(c[m]);
        if(x==c[m])
         
         {System.out.println("答案正確!");
        index1++;}
        else
         {System.out.println("答案錯誤!");
         index2++;
         }
        System.out.println("答對:"+index1+"道");
        System.out.println("答錯:"+index2+"道");
        m++;
         }
       line= br.readLine();
        }
       br.close();
       }
 
   catch (IOException e) {
         }
  
        

       
     }
}

從兩點開始到4點13,將近兩個小時一刻,其實僅僅是一個簡單的四則計算程式而已,也沒有什麼深入的知識點,其實如果能靜下心來,整理思路,將近一個小時應該就可以搞定,但是現實就是我做了整整兩個多小時。總結原因如下:

沒有一個完整的思路,做一點是一點,在想後面的時候,又要更改前面的程式碼,導致整個思路非常的混亂。

其次對於檔案中讀取的操作不熟練,再遇到特殊字元暫停的步驟中,卡頓時間過於長,其次也是最大的一個問題

就是最開始的時候直接在網上找了一個模板,在修改多次之後未果之後,再開始自己從頭開始編。這中間也浪費了很多時間。