Java语言程序设计原书第10版(基础版)编程练习题答案 第二章

2.1  (将摄氏度转换为华氏摄氏度)编写程序,从控制台读入double型的摄氏温度,然后将其转换2.1   (将摄氏温度转换为华氏温度)编写程序,从控制台读入double型的摄氏温度,然后将其转换为华氏温度,并且显示结果。转换公式如下所示:

             华氏温度 = (9.5)* 摄氏温度 + 32

import java.util.Scanner;

public class Exercise2_1 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a degree in Celsius: ");
        double celsius = input.nextDouble();
        double fahrenheit = (9.0 / 5) * celsius + 32;
        System.out.println(celsius + " Celsius is " + fahrenheit + " Fahrenheit");
    }
}

2.2(计算圆柱体的体积)编写程序,读入圆柱体的半径和高,并使用下列公式计算圆柱的体积:

面积 = 半径 × 半径 × p

                                                               体积 = 面积 × 高

import java.util.Scanner;

public class Exercise2_2 {
    public static void main(String[] args) {
        final double PI = 3.14159;
        Scanner input = new Scanner(System.in);
        
        System.out.print("Enter the radius and length of a cylinder: ");
        double radius = input.nextDouble();
        double height = input.nextDouble();
        
        double area = radius * radius * PI;
        double volume = area * height;
        
        System.out.println("The area is " + area);
        System.out.println("The volume is " + volume);
    }
}

2.3   (将英尺转换为米)编写程序,读入英尺数,将其转换为米数并显示结果。一英尺等于0.305米。

import java.util.Scanner;

public class Exercise2_3 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a value for feet: ");
        double feet = input.nextDouble();
        double meter = feet * 0.305;
        System.out.println(feet + " feet is " + meter + " meters");
    }
}

2.4  (将磅转换为千克)编写程序,将磅转换为千克数。程序提示用户输入磅数,然后转换成千克并显示结果。一磅等于0.454千克。

import java.util.Scanner;

public class Exercise2_4 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a number in pounds: ");
        double pound = input.nextDouble();
        double kilogram = pound * 0.454;
        System.out.println(pound + " pounds is " + kilogram + " kilograms");
    }
}

2.5  (财务应用程序:计算消费)编写一个程序,读入一笔费用与酬金率,计算酬金和总钱数。例如,如果用户输入10作为费用,15%作为酬金率,计算结果显示酬金为$1.5,总费用为$11.5。

import java.util.Scanner;

public class Exercise2_5 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter The subtotal and a gratuity rate: ");
        double subtotal = input.nextDouble();
        double gratuityRate = input.nextDouble();
        double gratuity = subtotal * gratuityRate * 0.01;
        double total = subtotal + gratuity;
        System.out.println("The gratuity is $" + gratuity + " and total is $" + total);
    }
}

2.6  (求一个整数各位数的和)编写程序,读取一个在0和1000之间的整数,并将该整数的各位数字相加。例如:整数是932,各位数字之和为14。

import java.util.Scanner;

public class Exercise2_6 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a number between 0 and 1000: ");
        int number = input.nextInt();

        int number1 = number / 100;
        int number2 = number / 10 % 10;
        int number3 = number % 10;

        int sum = number1  + number2 + number3;
        System.out.println("The sum of the digits is " + sum );
    }
}

*2.7  (求出年数)编写程序,提示用户输入分钟数&#x

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值