Java黑皮书课后题第3章:3.7(金融应用:整钱兑零)修改程序清单2-10,使之只显示非零的币值单位,用单词的单数形式显示一个单位,复数形式显示多于一个的单位的值
3.7(金融應(yīng)用:整錢兌零)修改程序清單2-10,使之只顯示非零的幣值單位,用單詞的單數(shù)形式顯示一個單位,復(fù)數(shù)形式顯示多于一個的單位的值
- 題目
- 題目概述
- 程序清單2-10(非本題代碼)
- 破題/思路:這道題我思路可能理解比較難,可以自行搜索看其他博客如何處理
- 代碼
題目
題目概述
3.7(金融應(yīng)用:整錢兌零)修改程序清單2-10,使之只顯示非零的幣值單位,用單詞的單數(shù)形式顯示一個單位,復(fù)數(shù)形式顯示多于一個的單位的值
1美元和1美分:1 dollar and 1 penny
2美元和3美分:2 dollars and 3 pennies
程序清單2-10(非本題代碼)
import java.util.Scanner;public class QingDan {public static void main(String[] args) {// Create a ScannerScanner input = new Scanner(System.in);// Receive the amountSystem.out.println("Enter an amount in int, for example 11.56");double amount = input.nextDouble();int remainingAmount = (int)(amount * 100);// Find the number of one dollarsint numberOfOneDollars = remainingAmount / 100;remainingAmount = remainingAmount % 100;// Find the number of quarters in the remaining amountint numberOfQuarters = remainingAmount / 25;remainingAmount = remainingAmount % 25;// Find the number of dimes in the remaining amountint numberOfDimes = remainingAmount / 10;remainingAmount = remainingAmount % 10;// Find the number of nickels in the remaining amountint numberOfNickels = remainingAmount / 5;remainingAmount = remainingAmount % 5;// Find the number of pennies in the remaining amountint numberOfPennies = remainingAmount;// Display resultsSystem.out.println("Your amount" + remainingAmount + " consists of");System.out.println(" " + numberOfOneDollars + " dollars");System.out.println(" " + numberOfQuarters + " quarters");System.out.println(" " + numberOfDimes+ " dimes");System.out.println(" " + numberOfNickels + " nickels");System.out.println(" " + numberOfPennies+ " pennies");} }破題/思路:這道題我思路可能理解比較難,可以自行搜索看其他博客如何處理
只需要理解它(清單2-10)的大體框架即可,這種改寫題因?yàn)榻o出了很多代碼相對也比較簡單
理解難點(diǎn)在中間幣值的轉(zhuǎn)換,自己帶幾個數(shù)進(jìn)去就能逐漸掌握每行代碼想表達(dá)的意思了
對于3.7,我的思路是先獲取到數(shù)據(jù)并進(jìn)行處理轉(zhuǎn)換后,在輸出部分每種幣值分三種情況(為0、為1、大于1)使用print()函數(shù)輸出內(nèi)容即可
如何在輸出的兩個幣值之間加and、留空格?
因?yàn)槊總€單位輸出都存在不確定性,不能直接print()函數(shù)中簡單地添加and并留出空格
筆者考慮到可以用boolean打標(biāo)進(jìn)行判斷
假設(shè)dollar前先標(biāo)記為true,那么后續(xù)所有輸出的地方都將這個標(biāo)記賦值為false
那么一旦讀到這個標(biāo)記是false,說明前面已經(jīng)有輸出,即前面留出空格并加上and
代碼
import java.util.Scanner;public class Test3_7 {public static void main(String[] args) {// 接收數(shù)據(jù)Scanner input = new Scanner(System.in);System.out.println("Enter an amount in int, for example 11.56");double amount = input.nextDouble();// 轉(zhuǎn)換單位+打標(biāo)int remainingAmount = (int)(amount * 100);boolean bool = true;// Find the number of one dollarsint numberOfOneDollars = remainingAmount / 100;remainingAmount = remainingAmount % 100;if(numberOfOneDollars > 1) {System.out.print(numberOfOneDollars + " dollars");bool = false;}else if(numberOfOneDollars == 1) {System.out.print(numberOfOneDollars + " dollar");bool = false;}// Find the number of quarters in the remaining amountint numberOfQuarters = remainingAmount / 25;remainingAmount = remainingAmount % 25;if(numberOfQuarters > 1 && bool == true) {System.out.print(numberOfQuarters + " quarters");bool = false;}else if(numberOfOneDollars == 1 && bool == true) {System.out.print(numberOfQuarters + " quarter");bool = false;}else if(numberOfQuarters > 1 && bool == false)System.out.println(" and " + numberOfQuarters + " quarters");else if(numberOfQuarters == 1 && bool == false)System.out.println(" and " + numberOfQuarters + " quarter");// Find the number of dimes in the remaining amountint numberOfDimes = remainingAmount / 10;remainingAmount = remainingAmount % 10;if(numberOfDimes > 1 && bool == true) {System.out.print(numberOfDimes + " dimes");bool = false;}else if(numberOfDimes == 1 && bool == true) {System.out.print(numberOfDimes + " dime");bool = false;}else if(numberOfDimes > 1 && bool == false)System.out.println(" and " + numberOfDimes + " dimes");else if(numberOfDimes == 1 && bool == false)System.out.println(" and " + numberOfDimes + " dimes");// Find the number of nickels in the remaining amountint numberOfNickels = remainingAmount / 5;remainingAmount = remainingAmount % 5;if(numberOfNickels > 1 && bool == true) {System.out.print(numberOfNickels + " nickels");bool = false;}else if(numberOfNickels == 1 && bool == true) {System.out.print(numberOfNickels + " nickel");bool = false;}else if(numberOfNickels > 1 && bool == false)System.out.println(" and " + numberOfNickels + " nickels");else if(numberOfNickels == 1 && bool == false)System.out.println(" and " + numberOfNickels + " nickel");// Find the number of pennies in the remaining amountint numberOfPennies = remainingAmount;if(numberOfPennies > 1 && bool == true)System.out.print(numberOfPennies + " pennies");else if(numberOfPennies == 1 && bool == true)System.out.print(numberOfPennies + " penny");else if(numberOfPennies > 1 && bool == false)System.out.println(" and " + numberOfPennies + " pennies");else if(numberOfPennies == 1 && bool == false)System.out.println(" and " + numberOfPennies + " penny");} }總結(jié)
以上是生活随笔為你收集整理的Java黑皮书课后题第3章:3.7(金融应用:整钱兑零)修改程序清单2-10,使之只显示非零的币值单位,用单词的单数形式显示一个单位,复数形式显示多于一个的单位的值的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java黑皮书课后题第3章:*3.5(给
- 下一篇: Java黑皮书课后题第3章:*3.8(对