2.1
import java.util.Scanner;
public class Test01 {
public static void main(String[] args) {
System.out.print("Enter a degree in Celsius:");
Scanner input=new Scanner(System.in);
double celsius=input.nextDouble();
System.out.println(celsius+" Celsius is "+((9.0/5)*celsius+32)+" Fahrenheit");
}
}
2.2
import java.util.Scanner;
public class Test02 {
public static void main(String[] args) {
System.out.print("Enter the radius and length of a cylinder:");
double pi = Math.PI;
Scanner input=new Scanner(System.in);
double radius=input.nextDouble();
double length=input.nextDouble();
System.out.println("The area is "+pi*Math.pow(radius,2));
System.out.println("The volume is "+pi*Math.pow(radius,2)*length);
}
}
2.3
import java.util.Scanner;
public class Test03 {
public static void main(String[] args) {
System.out.print("Enter a value for feet:");
Scanner input=new Scanner(System.in);
double feet=input.nextDouble();
System.out.println(feet+" feet is "+feet*0.305+" meters");
}
}
2.4
import java.util.Scanner;
public class Test04 {
public static void main(String[] args) {
System.out.print("Enter a number in pounds:");
Scanner input=new Scanner(System.in);
double pounds=input.nextDouble();
System.out.println(pounds+" pounds is "+pounds*0.454+" kilograms");
}
}
2.5
import java.util.Scanner;
public class Test05 {
public static void main(String[] args) {
System.out.print("Enter the subtotal and a gratuity rate:");
Scanner input=new Scanner(System.in);
double subtotal=input.nextDouble();
double rate=input.nextDouble()/100;
System.out.println("The gratuity is "+(subtotal*rate)+" and total is $"+(subtotal+rate));
}
}
2.6
import java.util.Scanner;
public class Test06 {
public static void main(String[] args) {
System.out.print("Enter a number between 0 and 1000 : ");
Scanner input=new Scanner(System.in);
int num=input.nextInt();
while(true){
if(num<1000&&num>0){
break;
}
else {
System.out.print("Error !!! please Enter a number between 0 and 1000 :");
num=input.nextInt();
}
}
int sum=0;
for (int i = num; num >0 ; ) { //999 9 99 9 9 9
sum+=num%10;
num=num/10;
}
System.out.println("The sum of the digits is "+sum);
}
}
2.7
package chaper2;
import java.util.Scanner;
/**
* 编写程序,提示用户输入分钟数(例如十亿)然后显示这些分钟代表多少年和多少天。
* 为了简化问题,假设一年有 365天。
* @author siyihe
* @create 2022-06-23 14:34
* @project javaexec0
*/
public class Years {
public static void main(String[] args) {