checkError使我的代码不起作用[Java]

时间:2014-11-15 19:38:22

标签: java if-statement

我需要有关此代码的帮助:

import java.util.Scanner;
public class Parking
{
    public static void main (String [] args)
    {
        //Defines constant integers
        final int quarterHour = 15;
        final int minutesInHour = 60;
        final int maxMin = 59;
        final int maxHour = 24;
        final int minMin = 0;
        final int minHour = 1;
        final int oneHour = 60;
        final int twoHours = 120;
        final int zero = 0;
        final int pastEighteen = 18;

        //Payments
        final int paymentFirstHour = 0;
        final int paymentSecondHour = 10;
        final int paymentQuarterHour = 3;
        final int paymentAfterSixPm = 20;
        final int afterTwoHours;
        final int roundUp;

        //Check if there's an error
        boolean checkError = false;

        //The payment the user has to pay
        int payment;

        //Input constructor
        Scanner scan = new Scanner(System.in);

        //Define user input, and get user's time entrance
        System.out.println("Enter your Entrance time: ");

        int inHour = scan.nextInt();
        int inMins = scan.nextInt();

        System.out.println("Enter your Exit time: ");

        int outHour = scan.nextInt();
        int outMins = scan.nextInt();

        int totalParkingInsideTime = (outHour * minutesInHour) - (inHour * minutesInHour) + 
        (outMins - inMins);
        System.out.println("You parked for : " + totalParkingInsideTime + " minutes ");

        //Calculate the parking cost Before  18:00 O'clock
        if ((inHour <= 18) && (checkError = false)){
            if (totalParkingInsideTime <= oneHour)
            {
                payment = paymentFirstHour;
                System.out.println("Total charge is : " + payment + " NIS ");
            }
            else if ((totalParkingInsideTime > oneHour) && totalParkingInsideTime <= twoHours)
            {
                payment = paymentSecondHour;
                System.out.println("Total charge is : " + payment + " NIS ");
            }
            else if (totalParkingInsideTime > twoHours){
                afterTwoHours = ((totalParkingInsideTime - twoHours) / quarterHour);

                if ( ((totalParkingInsideTime - twoHours) % quarterHour) ==  zero)
                {
                    payment = afterTwoHours * paymentQuarterHour;
                    System.out.println("Total charge is : " + payment + " NIS ");
                }
                else  
                {
                    payment = (afterTwoHours * paymentQuarterHour) + paymentQuarterHour;
                    System.out.println("Total charge is : " + payment + " NIS ");
                }
            }
       }     
       //Payment After 18:00 O'clock
       if ((inHour >= pastEighteen) && (checkError = false))
       {
           payment = paymentAfterSixPm;
           System.out.println("Total charge is : " + payment + " NIS ");
       }


       //Check for errors
       if ((inHour > maxHour) || (inHour < minHour)){
           System.out.println("Error! Illegal data.");
           checkError = true;
       }
       if ((inMins > maxMin) || (inMins < minMin)){
           System.out.println("Error! Illegal data.");
           checkError = true;
       }
       if ((outHour > maxHour) || (outHour < minHour)){
           System.out.println("Error! Illegal data.");
           checkError = true;
       }
       if ((outMins > maxMin) || (outMins < minMin)){
           System.out.println("Error! Illegal data.");
           checkError = true;
       }
       if((inHour > outHour) || (inMins > outMins)){
           System.out.println("Error! Entrance time must precede exit time.");
           checkError = true;
       }

    }
}

为什么阻止//Payment After 18:00 O'clock不执行?如果我取消checkError = false然后它确实有效,但它会显示错误和付款。我不希望这种情况发生。在18:00之前检查时,它也发生在之前的if语句中。想法?

2 个答案:

答案 0 :(得分:3)

您可能希望将条件更改为

if ((inHour >= pastEighteen) && (checkError == false))

否则,您要将false分配给checkError,而不是检查它是否已经为假。

答案 1 :(得分:1)

checkError = false是一项作业而不是您可能想要的考试checkError == false(请注意双等号)

有关运营商的更多信息,请参阅此tutorial