2015-08-08 30 views
-7

你好能有人幫我解決我面臨的問題:有人可以幫我完成我的java任務嗎?

由於某些原因,它不會在hoursEntered()操作後移動。我希望它去if if塊

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; 
     } 
    } 
} 

由於某種原因,該程序不會移動到下一步?

+0

請格式化你的代碼,然後將其粘貼到堆棧溢出編輯器,點擊「{}」按鈕,這樣看起來像一個真正的程序:) – Roberto

+1

請訪問[幫助],花點時間瞭解這個網站以及如何提問。總的來說,你需要努力解決問題,並告訴我們你做了什麼。在這種情況下,我建議您逐步瀏覽IDE(Eclipse,NetBeans等)中的代碼,並在沒有達到期望值的地方檢查變量。 –

+0

謝謝吉姆....對不起kind'a新到這,是的,我會通過幫助中心 –

回答

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; 

     } 
    } 
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; 
    ... 
} 

而且我想你應該重新編寫功能以允許遞歸(因爲我認爲你會要求用戶輸入一個有效的小時未定義的次數)。

+0

非常有幫助:) –

相關問題