java – 利润计算器的接收错误?
发布时间:2020-12-15 05:01:43 所属栏目:Java 来源:网络整理
导读:对 Java来说还是新手,我的任务是为一个纸质男孩制作一个利润计算器,但是我收到了这个错误: Enter the number of daily papers delivered: 50Enter the number of Sunday papers delivered: 35The amount collected for daily papers was: Exception in thre
对
Java来说还是新手,我的任务是为一个纸质男孩制作一个利润计算器,但是我收到了这个错误:
Enter the number of daily papers delivered: 50 Enter the number of Sunday papers delivered: 35 The amount collected for daily papers was: Exception in thread "main" java.util IllegalFormatConversionException: d != java.lang.Double at java.util.Formatter$FormatSpecifier.failConversion(Unknown Source) at java.util.Formatter$FormatSpecifier.printInteger(Unknown Source) at java.util.Formatter$FormatSpecifier.print(Unknown Source) at java.util.Formatter.format(Unknown Source) at java.io.PrintStream.format(Unknown Source) at java.io.PrintStream.printf(Unknown Source) at lab2b_MontelWhite.main(lab2b_MontelWhite.java:24) 这是我到目前为止: //Paper Boy's Wages Calculator import java.util.Scanner; public abstract class lab2b { public static void main(String[] args) { Scanner input = new Scanner( System.in); int x; int y; int result; System.out.print("Enter the number of daily papers delivered: "); x = input.nextInt(); System.out.print("Enter the number of Sunday papers delivered: "); y = input.nextInt(); double dailyResult = x * .3; System.out.printf("The amount collected for daily papers was: %dn",dailyResult); int SundayResult = y * 1; System.out.printf("The amount collected for Sunday papers was: %dn",SundayResult); double totalResult = dailyResult + SundayResult; System.out.printf("The total amount of money collected was: %dn",totalResult); double ProfitResult = (SundayResult + dailyResult)/2; System.out.printf("The paper boy's profit is: %dn",ProfitResult); } } 我究竟做错了什么? 解决方法
您可以查看
Formatter javadoc以查看所有数据类型格式字母.
从该页面,%d将数字格式化为“十进制整数”.这可能让你感到困惑.这实际上意味着“基数为10的整数”,就像使用(30)10来表示二进制数(11110)2.在转换类型中要考虑的重要事项是参数类别.该列中的“Integral”表示没有小数部分的整数数据类型,如int,long和BigInteger.另一方面,“浮点”意味着具有小数部分,例如double,float和BigDecimal.在你的情况下,你想要%f. 您还可以指定精度,即数字小数部分中显示的位数.既然你正在使用金钱,我将展示一个使用美元的例子: System.out.printf("The amount collected for Sunday papers was: $%.2fn",SundayResult); 哪个会打印像: The amount collected for Sunday papers was: $65.33 代替: The amount collected for Sunday papers was: $65.333333333 资源: > (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |