当前位置:文档之家› Java语言程序设计(基础篇)(第10版 梁勇著)第二章练习题答案

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

// 按照题目中提示 a=(v1-v0)/t double acceleration = (v1 - v0) / t; System.out.println("The average acceleration is " + acceleration);
}
}
// 第二章 P60 练习题 2.10 (科学:计算能量) import java.util.Scanner;
}
}
// 第二章 P59 练习题2.4 (将磅转换为千克) import java.util.Scanner;
public class PoundToKilogram {
public static void main(String[] args) { Scanner input = new Scanner(System.in);
System.out.println("The current time is " + hour + ":" + currentMinute + ":" + currentSecond);
}
}
// 第二章 P60 练习题 2.9 (物理:加速度) import java.util.Scanner;
public class Acceleration {
System.out.println("The gratuity is $" + choujin + " and total is $" + zongfeiyong);
}
}
// 第二章 P60 练习题2.6 (求一个整数各位数的和) import java.util.Scanner;
public class QiuHe {
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();
}
}
// 第二章 P60 练习题2.5 (财务应用程序:计算小费) import java.util.Scanner;
public class ChoujinHeZongqianshu {
public static void main(String[] args) { Scanner input = new Scanner(System.in);
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;
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);
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();
}
}
// 第二章 P59 练习题2.2 (计算圆柱体的体积) import java.util.Scanner;
public class VolumeOfCylinder {
public static void main(String[] args) { Scanner input = new Scanner(System.in);
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;
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;
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");
《Java语言程序设计(基础篇)》(第10版 梁勇 著) 第二章 基本程序设计 练习题答案
本人在自学编程过程中,发现本书答案很难找,找到的要么不完整、要么错误百出,所以我将自己所做的 练习题答案,提供给有需要者,供大家交流。本章答案均为本人一字一字所敲,答案均经过验证,虽为初学者, 但代码均按照书中规范要求书写,如有错误或更好的建议,请指正交流。
// 计算一年的总秒数 double totalSeconds,chusheng,siwang,qianru; totalSeconds = 365 * 24 * 60 * 60; // 分别计算一年中出生、死亡和移民迁入人口数 chusheng = totalSeconds / 7.0; siwang = totalSeconds / 13.0; qianru = totalSeconds / 45.0;
int years = (int)((minutes / 60 / 24) / 365); int days = (int)((minutes / rintln(minutes + " minutes is approximately " + years +
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();
public static void main(String[] args) { Scanner input = new Scanner(System.in);
相关主题