Using java conditional statements

Asked 2 years ago, Updated 2 years ago, 26 views

I want to create a salary calculation program that follows the following criteria.

I made this code, but I don't know what's wrong with it.

public class Salary { 
    public static void main(String[] args) {
        printSalary(10000, 160);
        printSalary(15000, 175);
        printSalary(9000, 180);
        printSalary(13000, 190);
    }
    public static void printSalary(int wage, int hours) {
        if (wage < 10000) {
            System.out.println ("[Error] The default hourly wage is less than 10,000 won.");
        } } else if (hours > 180) {
            System.out.println ("[Error] working hours exceeded 180 hours.");
        } } else if (hours>160) {
            System.out.printf ("[Salary] %d won"), (hours*160)+(hours*1.5)*(hours-160)));
        } } else {
            System.out.printf ("[Salary] %d won", wage * hours);
        }
    }
}

Error codes are

[Salary] KRW 1600000[Salary] Exception in thread "main" java.util.IllegalFormatConversionException: d!=java.lang.Double
    at java.base/java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4442)
    at java.base/java.util.Formatter$FormatSpecifier.printInteger(Formatter.java:2963)
    at java.base/java.util.Formatter$FormatSpecifier.print(Formatter.java:2918)
    at java.base/java.util.Formatter.format(Formatter.java:2689)
    at java.base/java.io.PrintStream.format(PrintStream.java:1209)
    at java.base/java.io.PrintStream.printf(PrintStream.java:1105)
    at ch01.Salary.printSalary(Salary.java:16)
    at ch01.Salary.main(Salary.java:6)

It pops up like this.

java

2022-09-20 10:54

1 Answers

Hello.

In most cases, if you read the error carefully, you will find the reason easier than you think.

Exception in thread "main" java.util.IllegalFormatConversionException: d != java.lang.Double

The location where the error occurred. They're saying "main thread.

The cause of the error. Illegal Format Conversion. There must have been a problem with the format process.

d is not java.lang.Double. I can tell by reading this much. d is a double type, but it is a problem caused by the formatting of %d(int.

This is a logic error that occurs when you predict that if you put int in the function, the calculation result will be int. Since 1.5 was multiplied by the calculation process, it was converted into a double type. If you output that part as a decimal, it will work normally.


2022-09-20 10:54

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.