java语言程序设计基础篇第二章程序
练习题答案
1
2020年4月19日
文档仅供参考
2.1(将摄氏温度转化为华氏温度) import java.util.*; public class test {
public static void main(String[] args) {
// TODO Auto-generated method stub Scanner input = new Scanner(System.in);
System.out.print(\); double Celsius = input.nextDouble();
double Fahrenheit;
Fahrenheit = (9.0/5) * Celsius + 32;
System.out.println(Celsius + \ +
Fahrenheit + \);
}
}
2.2(计算圆柱体的体积) import java.util.*;
public class test {
2
2020年4月19日
文档仅供参考
public static void main(String[] args) {
// TODO Auto-generated method stub Scanner input = new Scanner(System.in);
System.out.print(\
of a cylinder: \);
}
double radius = input.nextDouble(); double length =input.nextDouble();
double area = radius * radius * Math.PI; double volume = area * length;
System.out.println(\ + area); System.out.println(\ + volume);
}
2.3(将英尺转换为米) import java.util.Scanner;
public class test {
public static void main(String[] args) {
3
2020年4月19日
文档仅供参考
// TODO Auto-generated method stub Scanner input = new Scanner(System.in);
System.out.print(\); double feet = input.nextDouble();
double meters = feet * 0.305;
System.out.println(feet+ \ + meters +
\);
}
}
2.4(将磅转换为千克) import java.util.Scanner;
public class test {
public static void main(String[] args) {
// TODO Auto-generated method stub Scanner input = new Scanner(System.in);
System.out.print(\); double pounds = input.nextDouble();
4
2020年4月19日
文档仅供参考
double kilograms = pounds * 0.454;
System.out.println(pounds + \ +
kilograms + \);
}
}
2.5(财务应用程序:计算消费) import java.util.Scanner;
public class test {
public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in);
System.out.print(\
gratuity rate: \); double subtotal = input.nextDouble(); double Gratuity = input.nextDouble();
double gratuity = subtotal * Gratuity * 0.01; double total = gratuity + subtotal;
System.out.println(\ +
gratuity + \ + total);
2020年4月19日
5