Java语言程序设计(基础篇)(第10版 梁勇著)第二章练习题答案.pdf
文本预览下载声明
《Java语言程序设计(基础篇)》(第10版 梁勇 著)
第二章 基本程序设计 练习题答案
本人在自学编程过程中,发现本书答案很难找,找到的要么不完整、要么错误百出,所以我将自己所做的
练习题答案,提供给有需要者,供大家交流。本章答案均为本人一字一字所敲,答案均经过验证,虽为初学者,
但代码均按照书中规范要求书写,如有错误或更好的建议,请指正交流。
// 第二章 P59 练习题2.1 (将摄氏温度转为华氏温度)
import java.util.Scanner;
public class CelsiusToFahrenheit {
public static void main(String[] args) {
// 华氏温度和摄氏温度的转换公式为:华氏温度 = (9/5)*摄氏温度+32
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);
}
}
// 第二章 P59 练习题2.2 (计算圆柱体的体积)
import java.util.Scanner;
public class VolumeOfCylinder {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out .print(Enter the radius and length of a cylinder: );
double radius = input.nextDouble();
double length = input.nextDouble();
// 圆柱体面积=半径×半径×PI,圆柱体体积=面积×高
final double PI = 3.14159;
double area = radius * radius *PI;
double volume = area * length;
System.out .println(The area is + area);
System.out .println(The volume is + volume);
}
}
// 第二章 P59 练习题2.3 (将英尺转换为米)
import java.util.Scanner;
public class FeetToMeter {
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);
}
}
// 第二章 P59 练习题2.4 (将磅转换为千克)
import java.util.Scanner;
public class PoundToKilogram {
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
显示全部