简体   繁体   中英

Can't figure why my if statement is not working

I can't figure out why my if statement is not working. For some reason it always evaluates first if statement as true , and in result outputs the wrong value.

With numberOfDays = 19 it should output 4.95 , but instead it outputs 1.65 , because it evaluates first if statement as true and doesn't change the value of numberOf7DayPasses :

class TransitCalculator {

    int numberOfRides;
    int numberOfDays;
    double[] prices = { 2.75, 33.00, 127.00 };

    public TransitCalculator(int myRides, int myDays) {
        numberOfRides = myRides;
        numberOfDays = myDays;
    }

    public double unlimited7Price() {
        int numberOf7DayPasses = 1;

        if (numberOfDays % 7 != 0 && numberOfDays / 7 != 1 && numberOfDays / 7 > 1 && numberOfDays / 7 < 2) {
            numberOf7DayPasses = 2;
        } else if (numberOfDays % 7 != 0 && numberOfDays / 7 != 2 && numberOfDays / 7 > 2 && numberOfDays / 7 < 3) {
            numberOf7DayPasses = 3;
        } else if (numberOfDays % 7 != 0 && numberOfDays / 7 != 3 && numberOfDays / 7 > 3 && numberOfDays / 7 < 4) {
            numberOf7DayPasses = 4;
        } else if (numberOfDays % 7 != 0 && numberOfDays / 7 != 4 && numberOfDays / 7 > 4 && numberOfDays / 7 < 5) {
            numberOf7DayPasses = 5;
        } else if (numberOfDays % 7 == 0 && numberOfDays / 7 == 1) {
            numberOf7DayPasses = 1;
        } else if (numberOfDays % 7 == 0 && numberOfDays / 7 == 2) {
            numberOf7DayPasses = 2;
        } else if (numberOfDays % 7 == 0 && numberOfDays / 7 == 3) {
            numberOf7DayPasses = 3;
        } else if (numberOfDays % 7 == 0 && numberOfDays / 7 == 4) {
            numberOf7DayPasses = 4;
        } else if (numberOfDays % 7 == 0 && numberOfDays / 7 == 5) {
            numberOf7DayPasses = 5;
        }
        double priceOfOneRide = numberOf7DayPasses * prices[1] / numberOfRides;

        return priceOfOneRide;
    }
        
        
    public static void main(String[] args){
        TransitCalculator testPerson = new TransitCalculator(20, 19);
        System.out.println(testPerson.unlimited7Price());
    }
}

You say that you think it returns 1.65 because the first condition is evaluated true? So I guess you didn't use a debugger to step trough the code to see what really happens!

If the first condition would be evaluated to true, the output would be 3.3 (= 2*33/20) The output is 1.65 which means none of the conditions evaluates to true.

The first condition (and the 2nd and the 3th and the 4th) can't evaluate to true because they will always be false. The 1st condition contains:

numberOfDays / 7 > 1 && numberOfDays / 7 < 2

NumberOfDays is an int and because of that the result of numberOfDays / 7 is also an int. An int can't be greater than 1 and smaller than 2. So the condition will always be false. And in the 2nd, 3th and 4th condition, the numbers are different but they have the same problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM