开发者

Why are my if/else statments ignored?

开发者 https://www.devze.com 2023-03-29 04:39 出处:网络
So i\'m followingthe intro to java 6.092 course @ mit ocw. I\'m on lesson two, but nothing I try seems to make the program work right.

So i'm following the intro to java 6.092 course @ mit ocw. I'm on lesson two, but nothing I try seems to make the program work right.

i even noticed that the version of my code provided in the lesson 2 review at the beginning of lesson 3 has a similar problem. [edit]- the code from the lesson was incomplete cause i forgot to add a variable.

An employee gets paid (hours worked) × (base pay), for each hour up to 40 hours.

For every hour over 40, they get overtime = (base pay) × 1.5.

The base pay must not be less than the minimum wage ($8.00 an hour).

If it is, print an error. If the number of hours is greater than 60, print an error message.

Here is my try for a solution:

public static void main(String[] arguments) {
    int startOT = 40; 
    int maxH = 60;
    double otPay = 1.5;
    int emp1Hours = 35, emp2Hours = 47, emp3Hours = 73;
    double emp1Pay = 7.50, emp2Pay = 8.20, emp3Pay = 10.00;

    double swer;
    if (emp1Hours > startOT){
        swer = emp1Pay * startOT + (emp1Pay * otPay) * (emp1Hours - startOT);
        System.out.println("Employee John Doe makes " + "$" + swer + " this week!");
    }else if (emp1Hours > maxH){
        System.out.println("@John Doe ERROR! Hours worked is invalid!");
    }
    else if (emp1Pay < 8){
        System.out.println("Invalid hourly pay ;(");
    }
    else {
        swer = emp1Pay * emp1Hours;
        System.out.println("Employee John Doe makes " + "$" + swer + " this week!");
    }

    double swerzie;
    if (emp2Hours > startOT){
        swerzie = emp2Pay * startOT + (emp2Pay * otPay) * (emp2Hours - startOT);
        System.out.println("Employee Jane Doe makes " + "$" + swerzie + " this week!");
    }else if (emp2Hours > maxH){
        System.out开发者_StackOverflow社区.println("@Jane Doe ERROR! Hours worked is invalid!");
    }
    else if (emp2Pay < 8){
         System.out.println("Invalid hourly pay ;(");
    }
    else {
        swerzie = emp2Pay * emp2Hours;
        System.out.println("Employee Jane Doe makes " + "$" + swerzie + " this week!");
    }

    double answer;
    if (emp3Hours > startOT){
        answer = emp3Pay * startOT + (emp3Pay * otPay) * (emp3Hours - startOT);
        System.out.println("Employee malcom smith makes " + "$" + answer + " this week!");
    }else if (emp3Hours == 73){
        System.out.println("@malcom smith ERROR! Hours worked is invalid!");
    }
    else if (emp3Pay < 8){
         System.out.println("Invalid hourly pay ;("); }
    else {
        answer = emp3Pay * emp3Hours;
        System.out.println("Employee malcom smith makes " + "$" + answer + " this week!");
    }
}

And here's the output of the program:

-Invalid hourly pay ;(
-Employee Jane Doe makes $414.1 this week!
-Employee malcom smith makes $895.0 this week!


See, your code duplicates (triplicates) all logic and calculations This is, erm, suboptimal.

Imagine a procedure that takes hours_worked and base_pay as parameters and prints either the sum earned or a error message. Then apply it three times to three different employees.

Something along the lines of this:

from java.math import BigDecimal; // money is never float or double.

public static void calculateWage(String name, BigDecimal hourly_rate, int hours) {
    final int OVERTIME_THRESHOLD = 40;
    final int HOURS_LIMIT = 60;
    final BigDecimal MIN_HOURLY_RATE = new BigDecimal("8.00"); 
    // ^^^ again, never store money in a floating point form!


    if (hours > HOURS_LIMIT) {
        // what do you do?
    }

    if (hourly_rate.compareTo(MIN_HOURLY_RATE) < 0) { // less than
        // what do you do?
        // now think how to merge it with previous
    }

    if (hours > OVERTIME_THRESHOLD) {
        int overtime = hours - OVERTIME_THRESHOLD;
        // calculate two values and sum them up
    }
    else { // no overtime
        // calculate just one value
    }
}

public static void main(String[] args) {
    calculateWage("John", new BigDecimal("7.50"), 73);
    calculateWage("Jane", /* well, you see*/);
}

Isn't this code a bit more readable and neat? Yours can be, too.


The first part of the code looks fine:

 double emp1Pay = 7.50,...
 ...
 if (emp1Hours > startOT){
   ...
 else if (emp1Pay < 8){ System.out.println("Invalid hourly pay ;("); }

This prints out exactly what you'd expect if emp1Pay is <8:

"Invalid hourly pay ;(".

SUGGESTIONS:

1) Step through the code a line at a time under the Eclipse debugger. Follow what's happening - and why.

2) Consider "spreading out your code" a bit.

I hope your course examples don't use that coding style - bunching everything together makes it really hard to read. I can only imagine how difficult it must be for a beginner.

Here's a much better example of coding style:

http://java.sun.com/docs/codeconv/html/CodeConventions.doc3.html

3) Consider breaking your logic into a separate method. Instead of duplicating the code (once for each value), just call your new method three times (with three different sets of arguments - but just one block of code).

0

精彩评论

暂无评论...
验证码 换一张
取 消