1、2.1(将摄氏温度转化为华氏温度)import java.util.*;public class test public static void main(String args) / TODO Auto-generated method stubScanner 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;
2、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 stubScanner input = new Scanner(System.in);System.out.print(Enter the radius and length of a cylinder: );double radius
3、 = 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)
4、/ TODO Auto-generated method stubScanner 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 vo
5、id main(String args) / TODO Auto-generated method stubScanner 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.ut
6、il.Scanner;public class test public static void main(String args) / TODO Auto-generated method stubScanner 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 * Gr
7、atuity * 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 stubScanner input = new Scanner(System.in);System.out.pr
8、int(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;public class test public static void main(String args) / TODO Auto-generated met
9、hod stubScanner input = new Scanner(System.in);System.out.print(Enter the number of minutes: );double minutes = input.nextDouble();int years = (int)minutes / (60*24*365);int days = (int)minutes / (60*24) - (int)minutes / (60*24*365) * 365;System.out.println(minutes + minutes is approximately + years
10、 + years and + days + days);2.8(目前时间)import java.util.*;public class ShowXureentTime public static void main(String args) / TODO Auto-generated method stublong totalMilliseconds = System.currentTimeMillis();/得到1970年1月1日到现在毫秒数long totalSeconds = totalMilliseconds / 1000;/将总毫秒转化为总秒long currentSecond =
11、 totalSeconds % 60;/目前秒数long totalMinutes = totalSeconds / 60;/得到总分钟long currentMinute = totalMinutes % 60;/目前分钟数long totalHours = totalMinutes / 60;/得到总小时Scanner input = new Scanner(System.in);System.out.print(Enter the time zone offset to GMT: );long zone = input.nextInt();long currentHour = total
12、Hours % 24 + zone;/目前小时数if(currentHour 0) currentHour = currentHour + 24;System.out.println(Current time is + currentHour + : + currentMinute + : + currentSecond + GMT);/GMT世界时2.9(物理:加速度)import java.util.*;public class test public static void main(String args) / TODO Auto-generated method stubScanne
13、r input = new Scanner(System.in);System.out.print(Enter v0, v1 and t: );double v0 = input.nextDouble();double v1 = input.nextDouble();double t = input.nextDouble();double acceleration = (v1 - v0) / t;System.out.println(The average acceleration is + String.format(%.4f, acceleration);2.10(科学:计算能量)impo
14、rt java.util.*;public class test public static void main(String args) / TODO Auto-generated method stubScanner input = new Scanner(System.in);System.out.print(Enter the amount of water in kilograms: );double M = input.nextDouble();System.out.print(Enter the initial temperature: );double temperature1
15、 = input.nextDouble();System.out.print(Enter the final temperature: );double temperature2 = input.nextDouble();double energy = M * (temperature2 - temperature1) * 4184;System.out.println(The energy needed is + energy);2.1(人口统计)import java.util.*;public class test public static void main(String args)
16、 / TODO Auto-generated method stubScanner input = new Scanner(System.in);System.out.print(Enter the number of years: );int year = input.nextInt();int population = ;int temp = (365*12*60*60/7)-(365*12*60*60/13)+(365*12*60*60/45);/j = 诞生-死亡+移民迁入;for(int i = 1 ;i = year ;i + ) population = temp + popul
17、ation ;System.out.println(The population in + year + years is + population);2.12(物理:求出跑道长度)import java.util.*;public class test public static void main(String args) / TODO Auto-generated method stubScanner input = new Scanner(System.in);System.out.print(Enter speed and acceleration: );double speed =
18、 input.nextDouble();double acceleration = input.nextDouble();double length = speed * speed / (2 * acceleration);System.out.println(The minimum runway length for this airplane is + String.format(%.3f, length);2.13(财务应用程序:复利值)import java.util.*;public class test public static void main(String args) /
19、TODO Auto-generated method stubScanner input = new Scanner(System.in);System.out.print(Enter the monthly saving aomunt: );double monthly = input.nextDouble();double money = 0;for(int i = 1; i =2) in miles per hour: );double speed = input.nextDouble();double t = 35.74 + 0.6215 * Fahrenheit - 35.75 *
20、Math.pow(speed, 0.16) + 0.4275 * Fahrenheit * Math.pow(speed, 0.16);System.out.println(The win chill index is + String.format(%.5f, t);2.18(打印表格)import java.util.*;public class test public static void main(String args) / TODO Auto-generated method stubSystem.out.println(at + bt + pow(a, b);System.ou
21、t.println(1t + 2t + (int)Math.pow(1, 2);System.out.println(2t + 3t + (int)Math.pow(2, 3);System.out.println(3t + 4t + (int)Math.pow(3, 4);System.out.println(4t + 5t + (int)Math.pow(4, 5);System.out.println(5t + 6t + (int)Math.pow(5, 6);2.19(几何:三角形面积)import java.util.*;public class test public static
22、 void main(String args) / TODO Auto-generated method stubScanner input = new Scanner(System.in);System.out.print(Enter three points fir a trinangle: );double x1 = input.nextDouble();double y1 = input.nextDouble();double x2 = input.nextDouble();double y2 = input.nextDouble();double x3 = input.nextDou
23、ble();double y3 = input.nextDouble();double edge1= Math.pow(x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1), 0.5);double edge2= Math.pow(x3 - x2) * (x3 - x2) + (y3 - y2) * (y3 - y2), 0.5);double edge3= Math.pow(x3 - x1) * (x3 - x1) + (y3 - y1) * (y3 - y1), 0.5);double s =(edge1 + edge2 + edge3) / 2;dou
24、ble area = Math.pow(s * (s - edge1) * (s - edge2) * (s - edge3), 0.5);System.out.println(The area of the tringle is + String.format(%.1f, area);2.20(财务应用程序:计算利息)import java.util.*;public class test public static void main(String args) / TODO Auto-generated method stubScanner input = new Scanner(Syst
25、em.in);System.out.print(Enter balance and interst rate (e.g, 3 for 3%): );double balance = input.nextDouble();double annual = input.nextDouble();double interst = balance * ( annual / 1200);System.out.println(The interst is + String.format(%.5f, interst);2.21(财务应用:计算未来投资值)import java.util.*;public cl
26、ass test public static void main(String args) / TODO Auto-generated method stubScanner input = new Scanner(System.in);System.out.print(Enter investment amount: );double investment = input.nextDouble();System.out.print(Enter annual interest rate in percentage: );double interest = input.nextDouble();i
27、nterest = interest / 100 / 12;System.out.print(Enternumber of yuears: );double years = input.nextDouble();double value = investment * Math.pow(1 + interest), (years * 12);System.out.println(Accumulated value is $ + String.format(%.2f, value);2.22(财务应用:货币单位)import java.util.*;public class test public
28、 static void main(String args) / TODO Auto-generated method stubScanner input = new Scanner(System.in);System.out.print(Enter an amount in int, for example 1156: );int amount = input.nextInt();int remainingAmount = amount;int numberOfOneDollars = remainingAmount / 100;remainingAmount = remainingAmou
29、nt % 100;int numbersOfQuarters = remainingAmount / 25;remainingAmount = remainingAmount % 25;int numberOfDimes = remainingAmount / 10;remainingAmount = remainingAmount % 10;int numberOfNickels = remainingAmount / 5;remainingAmount = remainingAmount % 5;int numberOfPennies = remainingAmount;System.ou
30、t.println(Your amount + amount + consosts of);System.out.println( + numberOfOneDollars + dollars);System.out.println( + numbersOfQuarters + quarters);System.out.println( + numberOfDimes + dimes);System.out.println( + numberOfNickels + nickels);System.out.println( + numberOfPennies + pennies);2.23(驾驶
31、费用)import java.util.*;public class test public static void main(String args) / TODO Auto-generated method stubScanner input = new Scanner(System.in);System.out.print(Enter the driving distance: );double drivingDistance = input.nextDouble();System.out.print(Enter miles per gallon: );double milesPerGallon = input.nextDouble();System.out.print(Enter price per gallon: );double pricePerGallon = input.nextDouble();double costOfDriving = drivingDistance / milesPerGallon * pricePerGallon;System.out.print(The cost of driving is $ + String.format(%.2f, costOfDriving);