2016-10-08 140 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()可用于验证分配的值,只需记住将它们