java第二次作业

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

package School;

import java.util.Scanner;

public class A3 {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.print("输入圆的半径: ");
        double r = s.nextDouble();
        double ss = 3.14 * r * r;
        System.out.println("圆的面积是: " + ss);
    }
}

  

2.华氏温度和摄氏温度互相转换

package School;

import java.util.Scanner;
//摄氏度华氏度转换
public class A2 {
    public static void main(String[] args) {
        System.out.println("选择你要转换的温度:");
        System.out.print("摄氏度" + "\t" + "华氏度");
        Scanner s = new Scanner(System.in);
        String x = s.next();
        if ("摄氏度".equals(x)) {
            System.out.println("输入华氏度: ");
            Scanner a = new Scanner(System.in);
            double h = a.nextDouble();
            System.out.println("摄氏度: " + (h - 32.00) * 5.00 / 9.00);
        }
        if ("华氏度".equals(x)) {
            System.out.println("输入摄氏度: ");
            Scanner b = new Scanner(System.in);
            double ss = b.nextDouble();
            System.out.println("华氏度: " + (((ss * 9.00)/5.00  + 32.00)));
        }
    }
}

  

3.已知a,b均是整型变量,写出将a,b两个变量中的值互换的程序。

package School;

public class A4 {
    public static void main(String[] args) {
        int a = 5;
        int b = 8;
        System.out.println(a + "\t" + b);
        int x= a;
        a = b;
        b = x;
        System.out.println(a + "\t" + b);
    }
}

  

4.定义一个任意的5位整数,将它保留到百位,无需四舍五入

package School;

import java.util.Scanner;

public class A5 {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.print("输入五位整数: ");
        int x = s.nextInt();
        int w = x / 100 * 100;
        System.out.println("精确到百位: " + w);
    }
}

  

5.输入一个0~1000的整数,求各位数的和

package School;

import java.util.Scanner;
//输入数字求各位数的和
public class A1 {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.println("请输入数字: ");
        int x = s.nextInt();
        if (x>1000 || x<0) {
            System.out.println("输入数字不合法");
            return;
        }else if (x/1000 == 1) {
            System.out.println("数字和是:1");
            return;
        }else if (x/100 != 0) {
            System.out.println("数字的和是: " + (x/100+x%100/10+x%10));
            return;
        }else if (x/10 != 0) {
            System.out.println("数字的和是: " + (x/10 + x%10));
            return;
        }else {
            System.out.println("数字的和是: " + x);
        }
    }
}

  

6.定义一个任意的大写字母A~Z,转换为小写字母

package School;

public class A6 {
    public static void main(String[] args) {
        String str1 = "A";
        String str2 = "a";

        String str11 = str1.toLowerCase();
        String str22 = str2.toUpperCase();

        System.out.println(str1 + ": " + str11);
        System.out.println(str2 + ": " + str22);
    }
}

  

posted @ 2021-03-12 09:09  李芊  阅读(139)  评论(0编辑  收藏  举报