第四章第二十六题(金融应用:货币单位)(Financial application: monetary units)

  • *4.26(金融应用:货币单位)重写程序清单2-10,解决将float型值转换为int型值时可能会造成精度损失的问题。读取的输入值是一个字符串,比如“11.56”。你的程序应该应用indexOf和substring方法提取小数点前的美元数量,以及小数点后的美分数量。
    *4.26(Financial application: monetary units) Rewrite Listing 2.10, ComputeChange.java, to fix the possible loss of accuracy when converting a float value to an int value. Read the input as a string such as “11.56”. Your program should extract the dollar amount before the decimal point, and the cents after the decimal amount using the indexOf and substring methods.
  • 参考代码:
package chapter04;import java.util.Scanner;public class Code_26 {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.print("Enter an amount in double, for example 11.56: ");String amount = input.nextLine();int remainingAmount = (int)(Integer.parseInt(amount.substring(0, amount.indexOf('.'))) * 100+ Integer.parseInt(amount.substring(amount.indexOf('.')+1)));int numberOfOneDollars = remainingAmount / 100;remainingAmount = remainingAmount % 100;// Find the number of quarters in the remaining amountint numberOfQuarters = remainingAmount / 25;remainingAmount = remainingAmount % 25;int numberOfDimes = remainingAmount / 10;remainingAmount = remainingAmount % 10;int numberOfNickels = remainingAmount / 5;remainingAmount = remainingAmount % 5;int numberOfPennies = remainingAmount;System.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");input.close();}
}
  • 结果显示:
Enter an amount in double, for example 11.56: 11.56
Your amount 11.56 consists of11 dollars2 quarters 0 dimes1 nickels1 penniesProcess finished with exit code 0

第四章第二十六题(金融应用:货币单位)(Financial application: monetary units)相关推荐

  1. 第二章第十六题(几何:六边形面积)(Geometry: area of a hexagon)

    第二章第十六题(几何:六边形面积)(Geometry: area of a hexagon) 2.16(几何:六边形面积)编写程序,提示用户输入六边形的边长,然后显示它的面积.计算六边形面积的公式是: ...

  2. 第二章第二十二题(金融应用:货币单位)(Financial application: monetary units)

    *2.22(金融应用:货币单位)改写程序清单2-10,解决将double型值转换为int型值可能会造成精度损失问题.以整数值作为输入,其最后两位代表的是美分币值.例如:1156就表示的是11美元56美 ...

  3. 第五章第二十九题(显示日历)(Display calendars)

    第五章第二十九题(显示日历)(Display calendars) **5.29(显示日历)编写程序,提示用户输入年份和代表该年的第一天是星期几的数字,然后在控制台上显示该年的日历表.例如,如果用户输 ...

  4. 第七章第二十二题(计算一个字符串中大写字母的数目)(Calculates the number of uppercase letters in a string)

    第七章第二十二题(计算一个字符串中大写字母的数目)(Calculates the number of uppercase letters in a string) *7.22(计算一个字符串中大写字母 ...

  5. 第三章第七题(金融应用:整钱兑零)(Financial application: monetary units)

    3.7(金融应用:整钱兑零)修改程序清单2-10,使之只显示非零的币值单位,用单词的单数形式显示一个单位,例如1dollar and 1 penny(1美元和1美分):用单词的复数形式显示多于一个单位 ...

  6. Windows核心编程(笔记13) 第十六章--第二十六章

    改变下记录方式,只写自己觉得需要注意一下的防止出错的地方,或者一些特别重要的点,或者一些感悟. 第十六章 线程栈 第十七章 内存映射文件 1.注意写时复制在内存映射文件中的用处. 2.#pagma d ...

  7. 第六章第二十五题(将毫秒数转换成小时数、分钟数和秒数)(Convert milliseconds to hours, minutes, and seconds)

    **6.25(将毫秒数转换成小时数.分钟数和秒数)使用下面的方法头,编写一个将毫秒数转换成小时数.分钟数和秒数的方法. public static String convertMillis(long ...

  8. 第四章第十二题(十六进制转二进制)(Hex to binary)

    4.12(十六进制转二进制)编写一个程序,提示用户输入一个十六进制数,显示其对应的二进制数.对于不正确的输入数字,提示非法输入. 下面是一个运行示例: Enter a hex digit: B The ...

  9. 第六章第十五题(金融应用:打印税表)(Financial application: print a tax table)

    第六章第十五题(金融应用:打印税表)(Financial application: print a tax table) *6.15(金融应用:打印税表)程序清单3-5给出了计算税款的程序.使用税款的 ...

最新文章

  1. 2.9 学习率衰减-深度学习第二课《改善深层神经网络》-Stanford吴恩达教授
  2. 【安全】免密登陆SQLSERVER 之 Token 窃取
  3. centos mysql php tomcat_centos 一键部署安装nginx,mysql,php,apache,tomcat,redis等包合集
  4. 第二十六讲:基础一开放封闭原则
  5. 实践证明,SQL Server 2000 + 2005 + 2008,完全可以共存
  6. c++ double 只输出五位_C/C++编程笔记:C语言入门知识点(一),请收藏C语言最全笔记...
  7. Python爬虫项目--批量下载联合国粮食数据
  8. 计算机程序的构造和解释1构造过程抽象1.2过程与它们所产生的计算
  9. python人门指南小说-致Python初学者们 - Anaconda入门使用指南
  10. 小白用python处理excel文件-Python3操作Excel文件(读写)的简单实例
  11. 理财子公司成长的烦恼
  12. 计算机组成原理白中英知识点总结,计算机组成原理重点整理(白中英版)
  13. 织梦程序添加音乐上传功能
  14. VS2010上winform打包发布、打包安装程序(超全超详细)
  15. 巧用腾讯文档把pdf免费转word方法
  16. 云计算业务优势扩大,阿里云业务持续高速增长
  17. React Native Navigation
  18. java 热力图,热力图
  19. (zz)linux awk
  20. 查看tmp目录下的文件

热门文章

  1. ICMP控制报文协议
  2. Non-negative Matrix Factorization 非负矩阵分解
  3. matlab怎么绘制李萨如图形_在MatLab中使用for循环绘制图形
  4. 【论文阅读】SML:标准最大logits
  5. matlab 读excel表格文件 产生多个不同的.coe文件
  6. mysql的WAL技术是什么_我们常听到的WAL到底是什么
  7. 国内暂时可用的API集合
  8. 大创项目部分笔记(1)
  9. The requested URL was not found on the server. If you entered the URL manually please check your spe
  10. APB——(Advanced Peripheral Bus)