java 2 实用教程部分课后答案

java 2 实用教程部分课后答案

java各数值类型的取值范围

public class E {
    public static void main (String[] args){
        //byte的取值范围:-128至127
        System.out.println("byte的取值范围:"+Byte.MIN_VALUE+"至"+Byte.MAX_VALUE);
        //short的取值范围-32768至3.4028235E38
        System.out.println("short的取值范围"+Short.MIN_VALUE+"至"+Float.MAX_VALUE);
        //int的取值范围:-2147483648至2147483647
        System.out.println("int的取值范围:"+Integer.MIN_VALUE+"至"+Integer.MAX_VALUE);
        //long的取值范围:-9223372036854775808至9223372036854775807
        System.out.println("long的取值范围:"+Long.MIN_VALUE+"至"+Long.MAX_VALUE);
        //float的取值范围:1.4E-45至3.4028235E38
        System.out.println("float的取值范围:"+Float.MIN_VALUE+"至"+Float.MAX_VALUE);
        //double的取值范围:4.9E-324至1.7976931348623157E308
        System.out.println("double的取值范围:"+Double.MIN_VALUE+"至"+Double.MAX_VALUE);
    }
}

long数组简单应用

    public static void main(String[] args){
        long[] a = {1,2,3,4};
        long[] b = {100,200,300,400};
        b = a;
        System.out.println("数组b的长度:"+b.length);//数组b的长度:4
        System.out.println("b[0]="+b[0]);//b[0]=1
    }

int数组简单应用

    public static void main(String[] args){
        int [] a = {10,20,30,40}, b[] = {{1,2},{4,5,6,7}};
        b[0] = a;
        b[0][1] = b[1][3];
        System.out.println(b[0][3]);//40
        System.out.println(a[1]);//7
    }

初知Unicode编码

 public static void main(String[] args){
        char s0 = '你';
        int i0 = (int) s0;
        System.out.println("你的Unicode编码为:"+i0);//20230

        char s1 = '我';
        int i1 = (int)s1;
        System.out.println("我的Unicode编码为:"+i1);//25105

        char s2 = '他';
        int i2 = (int)s2;
        System.out.println("他的Unicode编码为:"+i2);//20182


    }

希腊字母

    public static void main(String[] args){
        int s = 0;
        for (int i = 945; i <= 969; i++) {
            s++;
            System.out.print((char)i+" ");//α β γ δ ε ζ η θ ι κ λ μ ν ξ ο π ρ ς σ τ υ φ χ ψ ω 
        }
    }

初识阶乘

    public static void main(String[] args){
        int i = 10;
        int j =0 ;
        int k =0 ;
        int num = 0;
        while(i > 0) {
            j = i-1;
            k = i;
            while(j>0){
                k *= j;
                j --;
            }
            num += k;
            i--;
        }
        System.out.println("1!+2!+3!+4!+5!+6!+7!+8!+9!+10!= "+num);//4037913
    }

素数求解

    public static void main(String[] args){
        int count = 0;
        //素数要大于等于2
        for (int i = 2; i <= 100; i++) {
            for (int j = 1; j <= i; j++) {
                if(i%j == 0){
                    count++;
                }
            }
            if(count <= 2){
                System.out.println(i);
            }
            count = 0;
        }
    }

计算1+1/2!+1/3!+1/4!+…前20项和

    public static void main(String[] args) {
        int j = 1;
        double num = 1.0;
        double num1 = 0.0;
        do{
            num *= j;
            num1 += 1/num;
            j++;
        }while(j <= 20);
        System.out.println(num1);//1.7182818284590455
    }

完数:一个数等于它所有因子的和

    public static void main(String[] args) {
        int k = 0;
        for (int i = 1; i <= 1000; i++) {
            for (int j = 1; j < i ; j++) {
                if(i%j == 0){
                    k += j;
                }
            }
            if( k == i){
                System.out.println("完数:"+k);
            }
             k = 0;
        }
    }

计算8+88+888+…

public static void main(String[] args) {
        double nums = 0;
        double num = 8;
        for (int i = 1; i <= 10 ; i++) {
            nums += num;
            num = num*10+8;
        }
        System.out.println(nums);
    }

求1+2+3+…+n < 8888 的最大整数

    public static void main(String[] args) {
        int num = 0;
        int nums = 0;
        while(true){
            nums += num;
            if(nums >= 8888){
                nums -= num;
                break;
            }
            num++;
        }
        System.out.println(num);//133
    }

你可能感兴趣的:(java 2 实用教程部分课后答案)