Java黑皮书课后题第2章:*2.22(金融应用:货币单位)改写程序清单2-10,解决将double转int可能会造成精度损失问题。以整数值作为输入,其最后两位代表的是美分币值
生活随笔
收集整理的這篇文章主要介紹了
Java黑皮书课后题第2章:*2.22(金融应用:货币单位)改写程序清单2-10,解决将double转int可能会造成精度损失问题。以整数值作为输入,其最后两位代表的是美分币值
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
*2.22(金融應用:貨幣單位)改寫程序清單2-10,解決將double轉int可能會造成精度損失問題。以整數值作為輸入,其最后兩位代表的是美分幣值
- 題目
- 題目概述
- 舉例
- 程序清單2-10
- 修改思路
- 代碼
題目
題目概述
*2.22(金融應用:貨幣單位)改寫程序清單2-10,解決將double轉int可能會造成精度損失問題。以整數值作為輸入,其最后兩位代表的是美分幣值
舉例
1156表示11美元56美分
程序清單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 double, 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" + amount + " 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");} }修改思路
找到從控制臺獲取數據的地方,更改賦值和下面語句變量即可
代碼
import java.util.Scanner;public class Test2_22 {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 1156");int remainingAmount = input.nextInt();// 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");} }總結
以上是生活随笔為你收集整理的Java黑皮书课后题第2章:*2.22(金融应用:货币单位)改写程序清单2-10,解决将double转int可能会造成精度损失问题。以整数值作为输入,其最后两位代表的是美分币值的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java黑皮书课后题第2章:*2.21(
- 下一篇: Java黑皮书课后题第2章:*2.23(