java语言程序设计与数据结构(基础篇)第二章答案
答案为本人自己求解,若有错误,还望海涵并及时告知。如有雷同,纯属巧合。
2.1
import java.util.Scanner; public class Welcome { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter a degree in Celsius:"); double celsius = input.nextDouble(); System.out.println(celsius+"Celsius is "+((9.0/5)*celsius+32)+" Fahrenheit"); } }
2.2
import java.util.Scanner; public class Welcome { public static void main(String[] args) { final double PI = 3.1415926; 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 * PI; System.out.println("The area is "+area); System.out.println("The volume is "+(area * length)); } }
2.3
import java.util.Scanner; public class Welcome { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter a value for feet:"); double feet = input.nextDouble(); System.out.println(feet+" feet is "+(feet * 0.305)+" meters"); } }
2.4
import java.util.Scanner; public class Welcome { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter a number in pounds:"); double pounds = input.nextDouble(); System.out.println(pounds+" pounds is "+(pounds * 0.454)+" kilograms"); } }
2.5
1 import java.util.Scanner; 2 3 public class Welcome 4 { 5 public static void main(String[] args) 6 { 7 Scanner input = new Scanner(System.in); 8 System.out.print("Enter the subtotal and a gratuity rate:"); 9 int fee = input.nextInt(); 10 int rate = input.nextInt(); 11 System.out.println("The gratuity is $"+fee * rate / 100.0+" and total is $"+(fee + fee * rate / 100.0)); 12 } 13 }
2.6
import java.util.Scanner; public class Welcome { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter a number between 0 and 1000:"); int num = input.nextInt(); int oneP = num % 10; int tenP = (num - oneP) % 100 / 10; int hunP = (num - oneP - tenP) / 100; System.out.println("The sum of the digits is "+(oneP+tenP+hunP)); } }
2.7
import java.util.Scanner; public class Welcome { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter the number of minutes:"); long min = input.nextLong(); long remainingday = min / (24 * 60); long day = remainingday % 365; long year = remainingday / 365; System.out.println(min+" minutes is approximately "+year+" years and "+day+" days"); } }
2.8
2.9
import java.util.Scanner; public class Welcome { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter v0,v1,and t:"); float v0 = input.nextFloat(),v1 = input.nextFloat(),t = input.nextFloat(); float a = (v1 - v0) / t; System.out.println("The average acceleration is "+a); } }
2.10
import java.util.Scanner; public class Welcome { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter the amount of water in kilograms:"); double amount = input.nextDouble(); System.out.print("Enter the initial temperature:"); double initialT = input.nextDouble(); System.out.print("Enter the final temperature:"); double finalT = input.nextDouble(); System.out.println("The energy needed is "+(amount * (finalT - initialT) * 4184)); } }
2.11
2.12
import java.util.Scanner; public class Welcome { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter speed and acceleration:"); double speed = input.nextDouble(),acceleration = input.nextDouble(); System.out.println("The mininum runway length for this airplane is "+(speed * speed / (2 * acceleration))); } }
2.13
2.14
import java.util.Scanner; public class Welcome { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter weight in pounds:"); double weight = input.nextDouble(); System.out.print("Enter height in inches:"); double height = input.nextDouble(); double kg = weight * 0.45359237,m = height * 0.0254; System.out.println("BMI is "+(kg / (m * m))); } }
2.15
import java.util.Scanner; public class Welcome { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter x1 and y1: "); double x1 = input.nextDouble(),y1 = input.nextDouble(); System.out.print("Enter x2 and y2: "); double x2 = input.nextDouble(),y2 = input.nextDouble(); double distance = Math.pow(Math.pow(x2 - x1,2 ) + Math.pow(y2 - y1, 2),0.5); System.out.println("The distance between the two points is "+distance); } }
2.16
import java.util.Scanner; public class Welcome { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter the length of the side: "); double length = input.nextDouble(); double area = 3.0 / 2 * Math.pow(3, 0.5) * length *length; System.out.println("The area of the hexagon is "+area); } }
2.17
2.18
2.19
2.20
2.21
2.22
2.23