有人可以帮助我完成我的java任务吗?

时间:2015-08-08 22:40:30

标签: java

您好,有人可以帮我解决我面临的问题:

由于某种原因,它不会在hoursEntered()操作后移动。我希望它转到if else块

package Exercises;

import java.util.Scanner;

public class ParkingCharges {

    static Scanner input = new Scanner(System.in);
    static double minimum_fee = 2.0;
    static double maximum_fee = 10.0;
    static double extra_per_HourCharge = 0.50;
    int daily_hours;

    public static void main(String[] args) {
        Display();
        hoursEntered();
        if (hoursEntered() <= 0.0 || hoursEntered() >24) {
            System.out.println("Invalid hours entered. Valid hours are 1 through 24");
            Display();
            hoursEntered();
        }
        if (hoursEntered() <= 3.0) {
            System.out.println("Minimum number of hours parked" + minimum_fee);
        } else {
            extraCharge();
        }
    }

    static void Display() {
        System.out.println(" Enter the number of hours parked: ");
    }

    public static double hoursEntered() {
        double numberOfHours = input.nextDouble();
        System.out.println(numberOfHours);
        return numberOfHours;
    }

    public static double extraCharge() {
        double extraChargeAmount = 0.0;
        extraChargeAmount = minimum_fee + (hoursEntered() - 3)*extra_per_HourCharge;
        if (extraChargeAmount >= 10.0) {
            extraChargeAmount = 10;
            return extraChargeAmount;
        } else {
            return extraChargeAmount;
        }
    }
}

由于某种原因,该程序不会进入后续步骤?

2 个答案:

答案 0 :(得分:1)

您多次拨打hoursEntered(),只需拨打一次电话:

double hour = hoursEntered();
if (hour <= 0.0 || hour > 24) {
    ...
    hoursEntered(); // <--- why?
} else if (hour <= 3.0) {
   ...
else {
    extraCharge(hour);
}

在extraCharge()方法中,只需发送此值:

public static double extraCharge(double hour) {
    ...
    extraChargeAmount = minimum_fee + (hour - 3)*extra_per_HourCharge;
    ...
}

我猜你应该重新编码你的函数以允许递归(因为我认为你会要求用户输入一个未定义的有效时间)。

答案 1 :(得分:1)

请参阅代码中的注释:

    import java.util.Scanner;

    public class ParkingCharges {

        static Scanner input = new Scanner(System.in);
        static double minimum_fee = 2.0;
        static double maximum_fee = 10.0;
        static double extra_per_HourCharge = 0.50;
        int daily_hours;

        public static void main(String[] args) {
            Display();
            //as Alexandro Sifuentes Díaz suggested
            double hoursParked = hoursEntered();

            //change if (that runs once) to  a loop
            while ((hoursParked <= 0.0) || (hoursParked >24)) {
                System.out.println("Invalid hours entered. Valid hours are 1 through 24");
                Display();
                hoursParked = hoursEntered();
            }

            if (hoursParked <= 3.0) {
                System.out.println("Minimum number of hours parked, fee is: " + minimum_fee);
            } else {
                //obtain and output the parking charge  
                System.out.println("Parking fee is " +extraCharge(hoursParked));

            }
        }

        static void Display() {
            System.out.println(" Enter the number of hours parked: \n");
        }

        public static double hoursEntered() {
            double numberOfHours = input.nextDouble();
            System.out.println(numberOfHours);
            return numberOfHours;
        }

        //use hoursParked obtained earlier
        public static double extraCharge(double hoursParked) {
            double extraChargeAmount = 0.0;
            extraChargeAmount = minimum_fee + ((hoursParked - 3)*extra_per_HourCharge);
            if (extraChargeAmount >= 10.0) {
                extraChargeAmount = 10;
                //removed un needed return and else else
            }
             return extraChargeAmount;

        }
    }