Java第二次作业

1.编写一个程序,定义圆的半径,求圆的面积.

package HomeWork;

public class work1 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int r = 2;
        double area;
        area = r * r * 3.14;
        System.out.println("半径是2时,圆的面积" + area);
    }

}

2.华氏温度和摄氏温度互相转换,从华氏度变成摄氏度你只要减去32,乘以5再除以9就行了,将摄氏度转成华氏度,直接乘以9,除以5,再加上32即行。(知识点:变量和运算符综合应用)

package HomeWork;

public class work2 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        double hua = 50;
        double she;
        she = (hua - 32) * 5 / 9;
        // 华转摄
        System.out.println("当华氏度是50时,转化为的摄氏度是" + she);
    }

}

package HomeWork;

public class work3 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        double hua;
        double she = 30;
        hua = she * 9 / 5 + 32;
        // 摄转华
        System.out.println("当摄氏度是30时,转化的华氏度是" + hua);
    }

}

3.已知a,b均是整型变量,写出将a,b两个变量中的值互换的程序。(知识点:变量和运算符综合应用)

package HomeWork;

public class work4 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int a = 5;
        int b = 8;
        int c;
        System.out.println("a=" + a + "\tb=" + b);
        System.out.println("互换后");
        c = a;
        a = b;
        b = c;
        System.out.println("a=" + a + "\tb=" + b);

    }

}

4.定义一个任意的5位整数,将它保留到百位,无需四舍五入(知识点:变量和运算符综合应用)

package HomeWork;

public class work5 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int a = 12345;
        int wan = 12345 / 10000;
        int qian = 12345 % 10000 / 1000;
        int bai = 12345 % 10000 % 1000 / 100;
        System.out.println(wan * 10000 + qian * 1000 + bai * 100);
    }

}

5.输入一个0~1000的整数,求各位数的和,例如345的结果是3+4+5=12注:分解数字既可以先除后模也可以先模后除(知识点:变量和运算符综合应用)

package HomeWork;

public class work6 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int a = 345;
        int ge = a % 10;
        int shi = a % 100 / 10;
        int bai = a / 100 % 10;
        int sum = ge + shi + bai;
        System.out.println("个位是" + ge + "\t十位是" + shi + "\t百位是" + bai);
        System.out.println(sum);
    }

}

6.定义一个任意的大写字母A~Z,转换为小写字母(知识点:变量和运算符综合应用)
定义一个任意的小写字母a~z,转换为大写字母

package HomeWork;

public class work7 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        char a = 'a';
        char b = 'a' - 32;
        System.out.println("a" + "\n转化后");
        System.out.println(b);

    }

}

package HomeWork;

public class work8 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        char a='A';
        char b='A'+32;
        System.out.println("A"+"\n转化后");
        System.out.println(b);
    }

}

posted @ 2021-03-12 11:31  张秋菊  阅读(45)  评论(0编辑  收藏  举报