java语言程序设计基础篇英文8版课后习题答案.doc
《java语言程序设计基础篇英文8版课后习题答案.doc》由会员分享,可在线阅读,更多相关《java语言程序设计基础篇英文8版课后习题答案.doc(64页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、public class Exercise1_2 public static void main(String args) System.out.println(Welcome to Java); System.out.println(Welcome to Java); System.out.println(Welcome to Java); System.out.println(Welcome to Java); System.out.println(Welcome to Java); public class Exercise1_4 public static void main(Stri
2、ng args) System.out.println(a a2 a3); System.out.println(1 1 1); System.out.println(2 4 8); System.out.println(3 9 27); System.out.println(4 16 64); public class Exercise1_6 public static void main(String args) System.out.println(1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9); public class Exercise1_8 public st
3、atic void main(String args) / Display area System.out.println(5.5 * 5.5 * 3.14159); / Display perimeter System.out.println(2 * 5.5 * 3.14159); import javax.swing.JOptionPane;public class Exercise2_1WithDialogBox / Main method public static void main(String args) / Enter a temperatur in Fahrenheit St
4、ring celsiusString = JOptionPane.showInputDialog(null, Enter a temperature in Celsius:, Exercise2_1 Input, JOptionPane.QUESTION_MESSAGE); / Convert string to double double celsius = Double.parseDouble(celsiusString); / Convert it to Celsius double fahrenheit = (9.0 / 5) * celsius + 32; / Display the
5、 result JOptionPane.showMessageDialog(null, The temperature is + fahrenheit + in Fahrenheit); import java.util.Scanner;public class Exercise2_2 public static void main(String args) Scanner input = new Scanner(System.in); / Enter radius of the cylinder System.out.print(Enter radius of the cylinder: )
6、; double radius = input.nextDouble(); / Enter length of the cylinder System.out.print(Enter length of the cylinder: ); double length = input.nextDouble(); double volume = radius * radius * 3.14159 * length; System.out.println(The volume of the cylinder is + volume); public class Exercise2_4 public s
7、tatic void main(String args) / Prompt the input java.util.Scanner input = new java.util.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); / Exercise2
8、_6.java: Summarize all digits in an integer 1000public class Exercise2_6 / Main method public static void main(String args) java.util.Scanner input = new java.util.Scanner(System.in); / Read a number System.out.print(Enter an integer between 0 and 1000: ); int number = input.nextInt(); / Find all di
9、gits in number int lastDigit = number % 10; int remainingNumber = number / 10; int secondLastDigit = remainingNumber % 10; remainingNumber = remainingNumber / 10; int thirdLastDigit = remainingNumber % 10; / Obtain the sum of all digits int sum = lastDigit + secondLastDigit + thirdLastDigit; / Displ
10、ay results System.out.println(The sum of all digits in + number + is + sum); public class Exercise2_8 public static void main(String args) java.util.Scanner input = new java.util.Scanner(System.in); / Enter an ASCII code System.out.print(Enter an ASCII code: ); int code = input.nextInt(); / Display
11、result System.out.println(The character for ASCII code + code + is + (char)code); import java.util.Scanner;public class Exercise2_10 /* Main method */ public static void main(String args) Scanner input = new Scanner(System.in); / Receive the amount entered from the keyboard System.out.print(Enter an
12、 amount in double, for example 11.56 ); double amount = input.nextDouble(); int remainingAmount = (int)(amount * 100); / Find the number of one dollars int numberOfOneDollars = remainingAmount / 100; remainingAmount = remainingAmount % 100; / Find the number of quarters in the remaining amount int n
13、umberOfQuarters = remainingAmount / 25; remainingAmount = remainingAmount % 25; / Find the number of dimes in the remaining amount int numberOfDimes = remainingAmount / 10; remainingAmount = remainingAmount % 10; / Find the number of nickels in the remaining amount int numberOfNickels = remainingAmo
14、unt / 5; remainingAmount = remainingAmount % 5; / Find the number of pennies in the remaining amount int numberOfPennies = remainingAmount; / Display results String output = Your amount + amount + consists of n + numberOfOneDollars + dollarsn + numberOfQuarters + quartersn + numberOfDimes + dimesn +
15、 numberOfNickels + nickelsn + numberOfPennies + pennies; System.out.println(output); import javax.swing.JOptionPane;public class Exercise2_12a public static void main(String args) / Obtain input String balanceString = JOptionPane.showInputDialog(null, Enter balance:); double balance = Double.parseDo
16、uble(balanceString); String interestRateString = JOptionPane.showInputDialog(null, Enter annual interest rate:); double annualInterestRate = Double.parseDouble(interestRateString); double monthlyInterestRate = annualInterestRate / 1200; double interest = balance * monthlyInterestRate; / Display outp
17、ut JOptionPane.showMessageDialog(null, The interest is + (int)(100* interest) / 100.0); import java.util.Scanner;public class Exercise2_12b public static void main(String args) Scanner input = new Scanner(System.in); / Obtain input System.out.print(Enter balance: ); double balance = input.nextDouble
18、(); System.out.print(Enter annual interest rate: ); double annualInterestRate = input.nextDouble(); double monthlyInterestRate = annualInterestRate / 1200; double interest = balance * monthlyInterestRate; / Display output System.out.println(The interest is + (int)(100* interest) / 100.0); import jav
19、a.util.Scanner;public class Exercise2_14 public static void main(String args) Scanner input = new Scanner(System.in); / Prompt the user to enter weight in pounds System.out.print(Enter weight in pounds: ); double weight = input.nextDouble(); / Prompt the user to enter height in inches System.out.pri
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java 语言程序设计 基础 英文 课后 习题 答案
限制150内