lundi 23 mars 2020

Problem with multiplying 10 by a number less then or equal to 40

The code for my project is:

    import java.util.*;

public class SalaryA
{
    public static void main (String[] args)
    {
        Scanner console = new Scanner(System.in);

        int week = 1;
        int totalNumberOfWeeks = 5;

        double totalCost = 0;

        while (week <= totalNumberOfWeeks)
        {
            double totalNumberOfHours;
            double total = 0;
            String weekStr = "";

            switch (week)
            {
                case 1 : weekStr = "Week 1"; break;
                case 2 : weekStr = "Week 2"; break;
                case 3 : weekStr = "Week 3"; break;
                case 4 : weekStr = "Week 4"; break;
                case 5 : weekStr = "Week 5"; break;
                default : weekStr = "Invalid"; break;
            }

            System.out.println("Input Number of Hours worked for "+weekStr+": ");
            totalNumberOfHours = console.nextDouble();

            if (totalNumberOfHours <= 40)
            {
                total = totalNumberOfHours * 10;
            } else if (totalNumberOfHours > 40);
            {
                total = 40 * 10 + (totalNumberOfHours - 40) * 15;
            }

            totalCost += total;
            week ++;

        }

        System.out.println("Total salary is: "+totalCost+"\n");
    }
}

If i set the number below or equal to 40 with an example of 15 which should come back as 150 but only comes back as 25. I set up a while loop to do that 5 times and it gives me 125 but the answer it should be giving me is 750. The else if statement works completely fine though which is what confuses me. I have tested this with a many numbers and it only effects below 41 (40-1). Everything else works fine.

Does anyone have a fix for this?

Aucun commentaire:

Enregistrer un commentaire