第四周课堂测试补做

  • 总结:
    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);
    }
}


2.

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);
            }
        }
    }
}


3.

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);
    }
}



4.

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();
    }
}


5.

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);
    }
}

posted on 2018-03-25 11:35  月半月半白勺  阅读(125)  评论(1编辑  收藏  举报

导航