java语言程序设计(基础篇)第二章程序练习题答案
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
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("Enter a degree in celsius: ");
double Celsius = input.nextDouble();
double Fahrenheit;
Fahrenheit = (9.0/5) * Celsius + 32;
System.out.println(Celsius+ " Celsius is"+ Fahrenheit + " Fahrenheit");
}
}
2.2(计算圆柱体的体积)
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("Enter the radius and length of a cylinder: ");
double radius = input.nextDouble();
double length =input.nextDouble();
double area = radius * radius * Math.PI;
double volume = area * length;
System.out.println("The area is " + area);
System.out.println("The volume is " + volume);
}
}
2.3(将英尺转换为米)
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("Enter a value for feet: ");
double feet = input.nextDouble();
double meters = feet * 0.305;
System.out.println(feet+ " feet is " + meters + " 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("Enter a number in pounds: ");
double pounds = input.nextDouble();
double kilograms = pounds * 0.454;
System.out.println(pounds + " pounds is " + kilograms
+ " 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("Enter the subtotal and gratuity rate: ");
double subtotal = input.nextDouble();
double Gratuity = input.nextDouble();
double gratuity = subtotal * Gratuity * 0.01;
double total = gratuity + subtotal;
System.out.println("The gratuity is $" + gratuity + " and total is " + total);
}
}
2.6(求一个整数个位数的和)
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("Enter a number between 0 and 1000: ");
int number = input.nextInt();
int sum = number % 10 + (number / 10) % 10 + (number / 100) % 10;
System.out.println("The sum of the digits is " + sum);
}
}
2.7(求出年数)
import java.util.Scanner;