2016-10-08 181 views
0

編輯:我重寫了一些代碼,但如果用戶輸入的時間不到一週,仍然無法計算出它的速率。我的新代碼如下。任何援助將收到感謝。 我必須編寫一個代碼來模擬在用戶輸入顏色,租用天數以及在兩種類型的車輛之間進行選擇時給出租車報價。每種類型有兩種費率,每週一次,每天一次。然後我必須找到最佳速度並輸出它。當我在下面的代碼中輸入white,economy和4時,它不輸出任何內容。我認爲在一週內我的匯率有誤,但我無法弄清楚如何解決這個問題。if else語句賦值

import java.util.Scanner; 
public class lab3 
{ 


    public static final double ECONOMY_DAILY = 25.5; 
    public static final double FULL_DAILY = 39.4; 
    public static final double ECONOMY_WEEKLY = 120.5; 
    public static final double FULL_WEEKLY = 216.25; 
    public static void main (String[] args) 
    { 
     Scanner Keyboard = new Scanner(System.in); 

     System.out.println("Enter the color of the vehicle:"); 
      //string the next input as color 
     String color = Keyboard.next(); 

     System.out.println ("Economy or Full:"); 
      // string the type of the vehicle 
     String Type = Keyboard.next(); 
      // to get the character value to uppercase for the switch statement 
     char FirstTypeLetter = Type.toUpperCase() 
     .charAt(0); 
     System.out.println("For how many days?"); 
      //set days as the next integer entered and calculate the amount of weeks and daysleftover using the/and % operators 
     int days = Keyboard.nextInt(); 
     int weeks = days/7; 
     int daysLeftOver = days%7; 
     double weeksRounded = ((days/7)*100)/100; 

    double rate1,rate2,rate3; 

     // create a switch using the variable defined earlier 
    switch (FirstTypeLetter) 
    { 
      // if the Type entry starts with an e 
      case 'F': 
       // calculate the 3 rates for full size using the full size constants (could have put this code anywhere above the next if statement.) 
      rate1 = weeksRounded * FULL_WEEKLY; 
      rate2 = (weeks * FULL_WEEKLY) + (days * FULL_DAILY); 
      rate3 = (days * ECONOMY_DAILY); 
      break; 
     case 'E': 
      // calculate all available rents for economy using the constants defined earlier 
    rate1 = weeksRounded * ECONOMY_WEEKLY; 
    rate2 = (weeks * ECONOMY_WEEKLY) + (days * ECONOMY_DAILY); 
    rate3 = (days * ECONOMY_DAILY); 

     break; 
     default: 
     System.out.println("Try Again!"); 
     rate1 = 0; 
     rate2 = 0; 
     rate3 = 0; 
     } 
      if ((rate1 < rate2) & (rate1 < rate3) & (rate1 != 0)) 
      { 
        // print out the first rate as well as the color and type that the user entered 
       System.out.printf("This is the best rate for a" + " " + color + " " + Type + " vehicle for" + " " + days + "days:" + "%.2f",rate1); 
      } 
       // if not, and if the second rate is cheapest 
      if ((rate2 < rate1) & (rate2 < rate3) & (rate1 != 0)) 
      { 
        // print out the second rate as well as the color and type that the user entered 
       System.out.printf("This is the best rate for a" + " " + color + " " + Type + " vehicle for" + " " + days + "days:" + "%.2f", rate2); 
      } 
        // if the third rate is cheapest then print out that rate 
      else if ((rate3 < rate2) & (rate3 < rate1) & (rate3 != 0)) 

       System.out.printf("This is the best rate for a" + " " + color + " " + Type + " vehicle for" + " " + days + "days:" + "%.2f", rate3); 
      } 



    } 
+1

確定你不想在'case F'中添加'break'? – home

+0

設置你的代碼的格式,你會看到所有的東西直到第一個brea k在你的第一個if塊中。 – tkausl

+0

您應該閱讀以下關於變量命名的[Java代碼約定](http://www.oracle.com/technetwork/java/codeconventions-135099.html)。像這樣的名字:'FirstTypeLetter','Type'和'Keyboard'令人困惑,因爲它們看起來像類的名字。此外,作爲代碼審查項目考慮更加面向對象的設計 - 您可以創建一個代表每種類型車輛的對象,其中包含每輛車的每週和每日費率。 –

回答

0

你的第二和第三如果stmt在你的1st if塊中。將其外

1

有一些東西,你需要考慮:

  • 存在的情況下,F和情況E之間缺少休息,這意味着 的速率F將與率E所取代
  • 比較費率的邏輯(第53行)不應僅應用於'E'的情況,該情況應該超出switch語句。
  • 沒有選項來驗證每日選項的費率(如果天數小於7,它將超出週期(檢查您的邏輯以計算'rate1')。
  • 您應該使用一個用於驗證分配給程序的值的調試器(使用JUnit或testNG進行自動單元測試將變得方便)
  • 如果您沒有調試器,System.out.println()可用於驗證分配的值,只需記住將它們