Java黑皮书课后题第4章:*4.26(金融应用:货币单位)重写程序清单2-10,解决将float型值转换为int型值时可能会造成精度损失的问题。读取的输入值是一个字符串,比如“11.56“
生活随笔
收集整理的這篇文章主要介紹了
Java黑皮书课后题第4章:*4.26(金融应用:货币单位)重写程序清单2-10,解决将float型值转换为int型值时可能会造成精度损失的问题。读取的输入值是一个字符串,比如“11.56“
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
*4.26(金融應(yīng)用:貨幣單位)重寫程序清單2-10,解決將float型值轉(zhuǎn)換為int型值時可能會造成精度損失的問題。讀取的輸入值是一個字符串,比如"11.56"
- 題目
- 題目概述
- 程序清單2-10
- 破題
- 代碼
題目
題目概述
*4.26(金融應(yīng)用:貨幣單位)重寫程序清單2-10,解決將float型值轉(zhuǎn)換為int型值時可能會造成精度損失的問題。讀取的輸入值是一個字符串,比如"11.56"
你的程序應(yīng)該應(yīng)用indexOf和substring方法提取小數(shù)點前的美元數(shù)量,以及小數(shù)點后的美分數(shù)量
程序清單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");} }破題
主要在Scanner句子(獲取用戶輸入數(shù)據(jù))后,到開始對輸入的數(shù)據(jù)進行轉(zhuǎn)換前這一部分需要修改
代碼
import java.util.Scanner;public class Test4_26 {public static void main(String[] args) {// Create a ScannerScanner input = new Scanner(System.in);// Receive the amountSystem.out.println("Enter an amount in float, for example 11.56");String str = input.next();// 通過indexOf()獲取小數(shù)點的位置(下標(biāo))int point = str.indexOf(".");// 截取小數(shù)點前后數(shù)字String st1 = str.substring(0, point);String st2 = str.substring(point+1);// 轉(zhuǎn)化為int類型,每位數(shù)字*100int num1 = Integer.parseInt(st1) * 100;int num2 = Integer.parseInt(st2);if(st2.length() == 1){num2 *= 10;}int remainingAmount = num1 + num2;// 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");} }總結(jié)
以上是生活随笔為你收集整理的Java黑皮书课后题第4章:*4.26(金融应用:货币单位)重写程序清单2-10,解决将float型值转换为int型值时可能会造成精度损失的问题。读取的输入值是一个字符串,比如“11.56“的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java黑皮书课后题第4章:*4.23(
- 下一篇: Java黑皮书课后题第5章:*5.1(统