日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java黑皮书课后题第4章:*4.26(金融应用:货币单位)重写程序清单2-10,解决将float型值转换为int型值时可能会造成精度损失的问题。读取的输入值是一个字符串,比如“11.56“

發布時間:2024/7/23 java 38 豆豆

*4.26(金融應用:貨幣單位)重寫程序清單2-10,解決將float型值轉換為int型值時可能會造成精度損失的問題。讀取的輸入值是一個字符串,比如"11.56"

  • 題目
    • 題目概述
    • 程序清單2-10
    • 破題
  • 代碼

題目

題目概述

*4.26(金融應用:貨幣單位)重寫程序清單2-10,解決將float型值轉換為int型值時可能會造成精度損失的問題。讀取的輸入值是一個字符串,比如"11.56"
你的程序應該應用indexOf和substring方法提取小數點前的美元數量,以及小數點后的美分數量

程序清單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句子(獲取用戶輸入數據)后,到開始對輸入的數據進行轉換前這一部分需要修改

  • 接收:更改為String類型接收
  • 通過indexOf()獲取小數點的位置
  • substring()截取小數點前、小數點后數字部分
  • 轉化為int類型,每位數字*100
  • 代碼

    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()獲取小數點的位置(下標)int point = str.indexOf(".");// 截取小數點前后數字String st1 = str.substring(0, point);String st2 = str.substring(point+1);// 轉化為int類型,每位數字*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");} }

    總結

    以上是生活随笔為你收集整理的Java黑皮书课后题第4章:*4.26(金融应用:货币单位)重写程序清单2-10,解决将float型值转换为int型值时可能会造成精度损失的问题。读取的输入值是一个字符串,比如“11.56“的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。