天天看點

personal project

words count program

統計文本檔案的字元數,單詞數和行數。

實作一個統計程式,他能正确的統計程式檔案中的字元數,單詞數和行數。

源碼連結

https://github.com/supipka/wc

心得:

在編寫代碼時首先考慮到要調傭哪個函數,在循環時是否會增加程式運作時間,在空字元,字元的處理問題上是否計算進去

測試用例

import java.util.Scanner;

public abstract class work{

   public static void main(String[] args) {

      Scanner reader = new Scanner(System.in);

      boolean i = true;

      int []num = new int[2];

      System.out.print( "請輸入每回合題目數量:" );

      int z = (int)reader.nextInt();

      while(i){

         int sco = 0;

         for(int w=0;w<z;w++){

            for(int i1=0;i1<2;i1++)

                num[i1] =  getnum();

            if(getnum()%2==1){

                System.out.print( num[0] + "+" + num[1] + "=" );

                int h = (int)reader.nextInt();

                int j = (int)(num[0]+num[1]);

                Printf( j, h );

                if (h==j)

                   sco++;

            }

            else{

                int a=0, b=1;

                if(num[0]-num[1]<0){a=1;b=0;}

                System.out.print( num[a] + "-" + num[b] + "=" );

                int j = (int)(num[a]-num[b]);

         }

         System.out.println("成績是:" + (int)(((float)sco)/((float)z)*100.0 ));

         System.out.println("是否繼續:1/0");

         int y = reader.nextInt();

         if (y==0)

            i = false;

      }

   }

   private static int getnum() {

      return (int)(Math.random()*10);

   public static void Printf(int x,int y){

      if(x==y)

         System.out.println("恭喜你,回答正确!");

      else

         System.out.println("回答錯誤,正确答案是:" + "  " + x);

}