我需要有关此代码的帮助:
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语句中。想法?
答案 0 :(得分:3)
您可能希望将条件更改为
if ((inHour >= pastEighteen) && (checkError == false))
否则,您要将false
分配给checkError,而不是检查它是否已经为假。
答案 1 :(得分:1)
checkError = false
是一项作业而不是您可能想要的考试checkError == false
(请注意双等号)
有关运营商的更多信息,请参阅此tutorial。