Java语言程序设计(基础篇)(第10版 梁勇著)第二章练习题答案

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
}
}
// 第二章 P59 练习题2.4 (将磅转换为千克) import java.util.Scanner;
public class PoundToKilogram {
public static void main(String[] args) { Scanner input = new Scanner(System.in);
}
}
// 第二章 P59 练习题2.3 (将英尺转换为米) import java.util.Scanner;
public class FeetToMeter {
public static void main(String[] args) { Scanner input = new Scanner(System.in);
public static void main(String[] args) { Scanner input = new Scanner(System.in);
System.out.print("Enter a number between 0 and 1000: "); int num1 = input.nextInt();
// 按照题目中所示公式 Q=M ×(最终温度-初始温度)× 4184 double energy = amountOfWater * (finalTem - initialTem) * 4184;
System.out.println("The energy needed is " + energy);
public class ComputeEnergy {
public static void main(String[] args) { Scanner input = new Scanner(System.in);
System.out.print("Enter the amount of water in kilograms: "); double amountOfWater = input.nextDouble(); System.out.print("Enter the initial temperature: "); double initialTem = input.nextDouble(); System.out.print("Enter the final temperature: "); double finalTem = input.nextDouble();
System.out.print("Enter a value for feet: "); double feet = input.nextDouble(); double meter = feet * 0.305;
System.out.println(feet + " feet is " + meter + " meters");
System.out.print("Enter a number in pounds: "); double pound = input.nextDouble(); double kilogram = pound * 0.454;
System.out.println(pound + " pounds is " + kilogram + " kilograms");
System.out.print("Enter the time zone offset to GMT: "); long timeZoneOffset = input.nextLong(); // 此处用long还是int?
long totalMilliseconds = System.currentTimeMillis(); long totalSeconds = totalMilliseconds / 1000; long currentSecond = totalSeconds % 60; long totalMinutes = totalSeconds / 60; long currentMinute = totalMinutes % 60; long totalHours = totalMinutes / 60; long currentHour = totalHours % 24; long hour = currentHour + timeZoneOffset;
// 第二章 P59 练习题2.1 (将摄氏温度转为华氏温度) import java.util.Scanner;
public class CelsiusToFahrenheit {
public static void main(String[] args) { // 华氏温度和摄氏温度的转换公式为:华氏温度 = (9/5)*摄氏温度+32 Scanner input = new Scanner(System.in);
// 按照题目中提示 a=(v1-v0)/t double acceleration = (v1 - v0) / t; System.out.println("The average acceleration is " + acceleration);
}
}
// 第二章 P60 练习题 2.10 (科学:计算能量) import java.util.Scanner;
// 计算一年的总秒数 double totalSeconds,chusheng,siwang,qianru; totalSeconds = 365 * 24 * 60 * 60; // 分别计算一年中出生、死亡和移民迁入人口数 chusheng = totalSeconds / 7.0; siwang = totalSeconds / 13.0; qianru = totalSeconds / 45.0;
" years and " + days + " days");
}
}
// 第二章 P60 练习题2.8 (当前时间) import java.util.Scanner;
(不知道此题的答案对否)
public class TheCurrentTime {
public static void main(String[] args) { Scanner input = new Scanner(System.in);
}
}
// 第二章 P60 练习题2.5 (财务应用程序:计算小费) import java.util.Scanner;
public class ChoujinHeZongqianshu {
public static void main(String[] args) { Scanner input = new Scanner(System.in);
public static void main(String[] args) { Scanner 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();
public class TheNumberOfYears {
public static void main(String[] args) { Scanner input = new Scanner(System.in);
System.out.print("Enter the number of minutes: "); long minutes = input.nextLong();
System.out.println("The gratuity is $" + choujin + " and total is $" + zongfeiyong);
}
}
// 第二章 P60 练习题2.6 (求一个整数各位数的和) import java.util.Scanner;
public class QiuHe {
System.out.print("Enter the subtotal and gratuity rate: "); double feiyong = input.nextDouble(); double choujinlv = input.nextDouble(); double choujin = feiyong * (choujinlv / 100); double zongfeiyong = feiyong + choujin;
int num2 = (int)(num1 / 100) + (int)(num1 / 10 % 10) + (int)(num1 % 10); System.out.println("The sum of the digits is " + num2);
}
}
// 第二章 P60 练习题2.7 (求出年数) import java.util.Scanner;
《Java语言程序设计(基础篇)》(第10版 梁勇 著) 第二章 基本程序设计 练习题答案
本人在自学编程过程中,发现本书答案很难找,找到的要么不完整、要么错误百出,所以我将自己所做的 练习题答案,提供给有需要者,供大家交流。本章答案均为本人一字一字所敲,答案均经过验证,虽为初学者, 但代码均按照书中规范要求书写,如有错误或更好的建议,请指正交流。
}
}
// 第二章 P59 练习题2.2 (计算圆柱体的体积) import java.util.Scanner;
public class VolumeOfCylinder {
public static void main(String[] args) { Scanner input = new Scanner(System.in);
}பைடு நூலகம்
}
// 第二章 P60 练习题2.11 (人口统计) import java.util.Scanner;
public class Population {
public static void main(String[] args) { Scanner input = new Scanner(System.in); int jishu = 312032486;
int years = (int)((minutes / 60 / 24) / 365); int days = (int)((minutes / 60 / 24) % 365); System.out.println(minutes + " minutes is approximately " + years +
System.out.print("Enter the radius and length of a cylinder: "); double radius = input.nextDouble(); double length = input.nextDouble();
// 圆柱体面积=半径×半径×PI,圆柱体体积=面积×高 final double PI = 3.14159; double area = radius * radius *PI; double volume = area * length; System.out.println("The area is " + area); System.out.println("The volume is " + volume);
System.out.println("The current time is " + hour + ":" + currentMinute + ":" + currentSecond);
}
}
// 第二章 P60 练习题 2.9 (物理:加速度) import java.util.Scanner;
public class Acceleration {
System.out.print("Enter a degree in Celsius: "); double celsius = input.nextDouble();
double fahrenheit = (9.0 / 5) * celsius + 32; System.out.println(celsius + " Celsius is " + fahrenheit + " Fahrenheit");
相关文档
最新文档