Java – receiving error of profit calculator?

As a novice to Java, my task is to make a profit calculator for a paper boy, but I received this error:

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)

This is what I have done so far:

//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: %d\n",dailyResult);
        int SundayResult = y * 1;

        System.out.printf("The amount collected for Sunday papers was: %d\n",SundayResult);
        double totalResult = dailyResult + SundayResult;

        System.out.printf("The total amount of money collected was: %d\n",totalResult);
        double ProfitResult = (SundayResult + dailyResult)/2;

        System.out.printf("The paper boy's profit is: %d\n",ProfitResult);
}
}

What on earth did I do wrong? I added doubles and I changed the name of "result" I'm just not sure what I did wrong

Solution

You can view the formatter Javadoc to see the format letters of all data types

From this page,% d formats numbers as decimal integers This may confuse you This actually means "an integer with base 10", just like using (30) 10 to represent binary number (11110) 2 An important consideration in converting types is parameter categories "Integral" in this column indicates integer data types without decimal parts, such as int, long and BigInteger On the other hand, "floating point" means having decimal parts, such as double, float, and BigDecimal In your case, you want% F

You can also specify the precision, that is, the number of digits displayed in the decimal part of the number Since you are using money, I will show an example of using dollars:

System.out.printf("The amount collected for Sunday papers was: $%.2f\n",SundayResult);

Which will print like:

The amount collected for Sunday papers was: $65.33

Substitute:

The amount collected for Sunday papers was: $65.333333333

resources:

> Formatter javadocs > Java Trail > Similar SO question

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>