代码之家  ›  专栏  ›  技术社区  ›  Nabeel A.Rahman

有人能帮我做java作业吗?

  •  -7
  • Nabeel A.Rahman  · 技术社区  · 9 年前

    你好,有人能帮我解决我面临的问题吗

    由于某种原因,它在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 回复  |  直到 9 年前
        1
  •  1
  •   c0der    9 年前

    请参见代码中的注释:

        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;
    
            }
        }
    
        2
  •  1
  •   Alex S. Diaz    9 年前

    你打了很多次电话 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;
        ...
    }
    

    我想你应该重新编码你的函数以允许递归(因为我认为你会要求用户输入一个有效的小时,但次数不确定)。