Java PTA题目——Chapter 3 Selections

197 阅读1分钟

Java PTA题目——Chapter 3 Selections

7-1 Convert Celsius to Fahrenheit(摄氏温度转换为华氏温度)

Write a program that reads a Celsius degree in a double value from the console, then converts it to Fahrenheit and displays the result.

The formula for the conversion is as follows:

fahrenheit = (9 / 5) * celsius + 32

重要提示:

必须使用 Main 作为主类名字!!!

输入格式:

输入一个double类型的摄氏度值。

输出格式:

对输入的摄氏温度值,输出转换的华氏温度值。

输入样例:

36.5

输出样例:

97.7

代码:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        double celsius=input.nextDouble();
        double fahrenheit = (9.0 / 5) * celsius + 32;
        System.out.println(fahrenheit);

    }

}

7-2 Compute the volume of a cylinder(计算圆柱体体积)

分数 3

Write a program that reads in the radius and length of a cylinder and computes the area and volume using the following formulas:

area = radius * radius * pi

volume = area * length

输入格式:

输入在一行中给出圆柱体的半径和高度,空格隔开。

输出格式:

输出输入值相应的圆柱体体积,小数点后保留2位。

输入样例:

5.5 12

输出样例:

1140.40

重要提示:

主类的名字必须用Main。

代码:


public class Main {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        double radius=input.nextDouble();
        double length=input.nextDouble();
        double area = radius * radius * Math.PI;
        double volume = area * length;

        System.out.printf("%.2f",volume);
    
    }

}