1

I have the user enter their location. and based on their location there is a pay difference. the if statements doesn't change the variable "pay" can someone help me?

import java.util.HashMap;
import javax.swing.*;

public class TimeSheet {

    public static void main(String[] args) {

        String loc;
        String[] startday = new String[7];
        String[] endday = new String[7];
        int x = 0;
        int y = 0;
        int num = 1;
        int location;
        double starttime;
        double endtime;
        double hour = 0;
        double total = 0;
        double pay = 0;
        double totalpay;

        loc = JOptionPane.showInputDialog(null, "Enter your Work Location");
        location = Integer.parseInt(loc);

        if (location == 100 && location < 200 ) {
            pay = 10.00;
        }
        else if (location == 200 && location < 300) {
            pay = 7.50;
        }
        else if (location == 300 && location < 400) {
            pay = 9.25;
        }
        else if (location == 400 && location < 500) {
            pay = 13.50;
        }
        else if (location == 500 && location < 600) {
            pay = 8.00;
        }

        HashMap<String, Double> map = new HashMap<String, Double>();
        map.put("1",9.0);
        map.put("2",9.5);
        map.put("3",10.0);
        map.put("4",10.5);
        map.put("5",11.0);
        map.put("6",11.5);
        map.put("7",12.0);
        map.put("8",12.5);
        map.put("A",13.0);
        map.put("B",13.5);
        map.put("C",14.0);
        map.put("D",14.5);
        map.put("E",15.0);
        map.put("F",15.5);
        map.put("G",16.0);
        map.put("H",16.5);
        map.put("I",17.0);

        while (x < 7 && y < 7) {

            startday[x] = JOptionPane.showInputDialog(null, "Enter Day "+num+" 's Starting code");
            endday[y] = JOptionPane.showInputDialog(null, "Enter Day "+num+" 's Ending code");

            starttime = map.get(startday[x]);
            endtime = map.get(endday[y]);

            hour = endtime - starttime;
            total = total + hour;
            totalpay = total*pay;

            JOptionPane.showMessageDialog(null, "total hour" + total);
            JOptionPane.showMessageDialog(null, "total pay for the week is " + totalpay);

            x++;
            y++;
            num++;


        }

    }

}
4

6 回答 6

3

your conditions are like:

else if (location == 200 && location < 300) {

translate to english

if location is exactly 200 and less than 300.

You probably want to change it to

if location is greater or equals than 200 and less than 300.

于 2012-12-04T19:05:45.310 回答
1

This might be why your if-block is not working as expected:

if (location == 100 && location < 200 ) {
    pay = 10.00;
}
else if (location == 200 && location < 300) {
    pay = 7.50;
}
else if (location == 300 && location < 400) {
    pay = 9.25;
}
else if (location == 400 && location < 500) {
    pay = 13.50;
}
else if (location == 500 && location < 600) {
    pay = 8.00;
}

Depending on exactly how you are defining the location system, it should be either:

if (location < 100 ) {
    // Do something with pay here
    pay = 0.00;
}
else if (location < 200 ) {
    pay = 10.00;
}
else if (location < 300) {
    pay = 7.50;
}
else if (location < 400) {
    pay = 9.25;
}
else if (location < 500) {
    pay = 13.50;
}
else if (location < 600) {
    pay = 8.00;
}
else {
    // You should probably have some error-handling here
    // as fail-safe default for defensive programming
}

Or

if (location == 100) {
    pay = 10.00;
}
else if (location == 200) {
    pay = 7.50;
}
else if (location == 300) {
    pay = 9.25;
}
else if (location == 400) {
    pay = 13.50;
}
else if (location == 500) {
    pay = 8.00;
}
else {
    // You should probably have some error-handling here
    // as fail-safe default for defensive programming
}
于 2012-12-04T19:07:42.410 回答
1

A simple Programmer 101 error -- you used AND when you should have used OR. Any value not 100, 200,300,400, 500 will fail because you want it to be 100 AND <200 etc. See below:

if (location == 100 || location < 200 ) {     
    pay = 10.00;
}
else if (location == 200 || location < 300) { 
    pay = 7.50;
}
else if (location == 300 || location < 400) {
    pay = 9.25;
}
else if (location == 400 || location < 500) {
    pay = 13.50;
}
else if (location == 500 || location < 600) {
    pay = 8.00;
}
于 2012-12-04T19:08:38.797 回答
0

Your if conditions look really odd. Did you mean to say >= rather than ==?

if (location == 100 && location < 200 ) {
             ^^ should this be >= ?

By the way, half of these checks are unnecessary:

    if (location < 100) {
    }
    else if (location < 200 ) {
        pay = 10.00;
    }
    else if (location < 300) {
        pay = 7.50;
    }
    else if (location < 400) {
        pay = 9.25;
    }
    else if (location < 500) {
        pay = 13.50;
    }
    else if (location < 600) {
        pay = 8.00;
    }
于 2012-12-04T19:05:44.830 回答
0

All your if statements contain && such as the following: (location == 100 && location < 200). Those should be replaced with ||.

&& = AND operator
|| = OR operator

See the following changes bellow:

if (location == 100 || location < 200 ) {
    pay = 10.00;
}
else if (location == 200 || location < 300) {
    pay = 7.50;
}
else if (location == 300 || location < 400) {
    pay = 9.25;
}
else if (location == 400 || location < 500) {
    pay = 13.50;
}
else if (location == 500 || location < 600) {
    pay = 8.00;
}
于 2012-12-04T19:12:48.167 回答
0

You don't really need the if-else mess for this. A Map of locations would clean it up quite a bit.

Map<String, Double> locMap = new HashMap<String, Double>();
locMap.put("100", 7.77);
locMap.put("500", 5.55);

pay = locMap.get(locStringFromInput);
于 2012-12-04T19:24:44.143 回答