第四周課堂測試補作

  • 總結:
    P29頁的題要求用本身的學號創建一個文件,代碼爲Example2_5的內容。因爲標識符不能以數字開頭,因此當時就一直調試錯誤,也沒有發現這個問題。

    使用Integer.parseInt(arg[0])來實現string到int的轉換
    循環與遞歸:
public class SumofRecur {
     public static void main(String args[]) {
        int [] tmp = new int [args.length];
        for(int i=0; i<args.length; i++) {
            tmp[i] = Integer.parseInt(args[i]);
         }
        int sum = 0;
        for (int i=1;i<=tmp[0];i++) {
            sum += fact(i);
        }
        System.out.println(sum);
    }

    public static int fact(int n) {
        if (n == 0)
             return 1;
         else
             return n * fact(n-1);
    }

使用JDB調試時,經過命令行傳入參數的方法:編程

jdb -classpath .:./bin HelloJDB 參數

  • 第二章編程題:
public class  work1 {
    public static void main (String args[ ]) {
        char chinaWord1='你';
        char chinaWord2='我';
        char chinaWord3='他';
        System.out.println("漢字:"+chinaWord1+"的位置:"+(int)chinaWord1);
        System.out.println("漢字:"+chinaWord2+"的位置:"+(int)chinaWord2);
        System.out.println("漢字:"+chinaWord3+"的位置:"+(int)chinaWord3);
    }
}

public class work2 {
    public static void main(String args[]) {
        char word1 = 'a';
        char word2 = 'z';
        char temp = '*';
        int m = (int) word1, n = (int) word2;
        int i;
        for (i = m; i <= n; i++) {
            temp = (char) i;
            System.out.println(temp);
        }
    }
}

  • 第三章編程題:
public class SumofRecur {
     public static void main(String args[]) {
        int [] tmp = new int [args.length];
        for(int i=0; i<args.length; i++) {
            tmp[i] = Integer.parseInt(args[i]);
         }
        int sum = 0;
        for (int i=1;i<=tmp[0];i++) {
            sum += fact(i);
        }
        System.out.println(sum);
    }

    public static int fact(int n) {
        if (n == 0)
             return 1;
         else
             return n * fact(n-1);
    }
}

public class work3 {
    public static void main(String[] args){
        int sum = 0;
        for(int i = 2; i < 100; i++){
            boolean isPrime = true;
            for(int j = 2; j < i; j++){
                if(i % j == 0){
                    isPrime = false;
                    break;
                }
            }
            if(isPrime){
                System.out.println(i);
            }
        }
    }
}

public class work4 {
    public static void main(String args[]) {
        int [] tmp = new int [args.length];
        for(int i=0; i<args.length; i++) {
            tmp[i] = Integer.parseInt(args[i]);
        }
        double sum = 0.00;
        for (int i=1;i<=tmp[0];i++) {
            sum += fact(i);
        }
        System.out.println(sum);

    }
    public static double fact(double n) {
        if (n == 1)
            return 1;
        else
            return 1/n * fact(n-1);
    }
}


public class work5 {
    public static void main(String[] args)
    {
        int s;
        for(int i=1;i<=1000;i++)
        {
            s=0;
            for(int j=1;j<i;j++)
                if(i % j==0)
                    s=s+j;
            if(s==i)
                System.out.print(i+" ");
        }
        System.out.println();
    }
}

public class work6 {
    public static void main(String args[]) {
        int [] tmp = new int [args.length];
        for(int i=0; i<args.length; i++) {
            tmp[i] = Integer.parseInt(args[i]);
        }
        int sum = 0;
        for (int i=1;i<=tmp[0];i++) {
            sum += fact(i);
        }
        System.out.println(sum);
    }

    public static int fact(int n) {
        if (n == 1)
            return 8;
        else
            return 8+10*fact(n-1);
    }
}

public class work7 {
    public static void main(String args[]) {
        int i,sum = 0;
        for ( i = 1; sum < 8888; i++) {
            sum += i;
        }
        System.out.println(i-1);
    }
}

相關文章
相關標籤/搜索