java语言程序设计(基础篇)第二章程序练习题答案.docx_第1页
java语言程序设计(基础篇)第二章程序练习题答案.docx_第2页
java语言程序设计(基础篇)第二章程序练习题答案.docx_第3页
java语言程序设计(基础篇)第二章程序练习题答案.docx_第4页
java语言程序设计(基础篇)第二章程序练习题答案.docx_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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; 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 = 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 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 void 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.util.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 * 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 stubScanner 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;public class test public static void main(String args) / TODO Auto-generated method 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 + 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 = 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 = totalHours % 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 stubScanner 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(科学:计算能量)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 amount of water in kilograms: );double M = input.nextDouble();System.out.print(Enter the initial temperature: );double temperature1 = 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) / TODO Auto-generated method stubScanner input = new Scanner(System.in);System.out.print(Enter the number of years: );int year = input.nextInt();int population = 312032486;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 + population ;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 = 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) / 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 * 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.out.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 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.nextDouble();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;double 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(System.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 class 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();interest = 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 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 = remainingAmount % 100;int numbersOfQuarters = remainingAmount / 25;remainingAmount = remainingAmount % 25;int numberOfDimes = remainingAmount / 10;remainingAmount = remainingAmount % 10;int numberOfNickels = remainingAm

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论