Java黑皮书课后题第3章:3.7(金融应用:整钱兑零)修改程序清单2-10,使之只显示非零的币值单位,用单词的单数形式显示一个单位,复数形式显示多于一个的单位的值
生活随笔
收集整理的這篇文章主要介紹了
Java黑皮书课后题第3章:3.7(金融应用:整钱兑零)修改程序清单2-10,使之只显示非零的币值单位,用单词的单数形式显示一个单位,复数形式显示多于一个的单位的值
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
3.7(金融應用:整錢兌零)修改程序清單2-10,使之只顯示非零的幣值單位,用單詞的單數形式顯示一個單位,復數形式顯示多于一個的單位的值
- 題目
- 題目概述
- 程序清單2-10(非本題代碼)
- 破題/思路:這道題我思路可能理解比較難,可以自行搜索看其他博客如何處理
- 代碼
題目
題目概述
3.7(金融應用:整錢兌零)修改程序清單2-10,使之只顯示非零的幣值單位,用單詞的單數形式顯示一個單位,復數形式顯示多于一個的單位的值
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)的大體框架即可,這種改寫題因為給出了很多代碼相對也比較簡單
理解難點在中間幣值的轉換,自己帶幾個數進去就能逐漸掌握每行代碼想表達的意思了
對于3.7,我的思路是先獲取到數據并進行處理轉換后,在輸出部分每種幣值分三種情況(為0、為1、大于1)使用print()函數輸出內容即可
如何在輸出的兩個幣值之間加and、留空格?
因為每個單位輸出都存在不確定性,不能直接print()函數中簡單地添加and并留出空格
筆者考慮到可以用boolean打標進行判斷
假設dollar前先標記為true,那么后續所有輸出的地方都將這個標記賦值為false
那么一旦讀到這個標記是false,說明前面已經有輸出,即前面留出空格并加上and
代碼
import java.util.Scanner;public class Test3_7 {public static void main(String[] args) {// 接收數據Scanner input = new Scanner(System.in);System.out.println("Enter an amount in int, for example 11.56");double amount = input.nextDouble();// 轉換單位+打標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");} }總結
以上是生活随笔為你收集整理的Java黑皮书课后题第3章:3.7(金融应用:整钱兑零)修改程序清单2-10,使之只显示非零的币值单位,用单词的单数形式显示一个单位,复数形式显示多于一个的单位的值的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java黑皮书课后题第3章:*3.5(给
- 下一篇: Java黑皮书课后题第3章:*3.8(对