-4

问题:的Java:在错误变量的作用域if语句

的互联网服务提供提供三种不同的包月套餐:

套餐1:每月$ 15.95长达10小时的服务。额外的时间是每小时$ 2.00。套餐2:每月20.95美元,长达20小时的服务。额外的时间是每小时$ 1.00。套餐3:每月30.99美元无限次使用。

写一个程序,执行以下操作:

问哪个计划他们的用户。

询问用户月份编号(1-12)他们正在付款的月份。

询问用户他们使用了多少小时。

显示他们账单的成本。

还显示多少钱,如果他们购买的包2或3包1级的客户会会救了,多少钱套餐2名的客户将有可能挽救,如果他们购买的包3

如果没有储蓄,不要显示此消息。

输入验证:

计划数不能为负数。

计划编号必须是值[1,3]。

小时数不能为负数。

小时数不能超过给定月份的总小时数。

有30天的月份有720个小时,31天的月份有744小时,2月有28天有672小时(不用担心闰年)。

验证用户未输入的值大于给定月份的总小时数。

请参见下表。

月,日,小时。

月,31,744

日,28日,672

月,31,744

月,30,720

日,31日,744

June,30,720.

744年7月31日。

月,31,744

月,30,720

月,31,744

月,30,720

月,31,744。

我的问题。

我遇到了我的变量问题。我知道有更好的方法来编写这个程序,但是你应该记住,这是我第四周用Java编码。

我用了太多if语句,并且当我尝试编译它时;我用某些变量得到了12个错误。我假设变量的变量范围被某个if语句阻塞。因为编译器要求初始化这些变量,即使它们之前已经初始化了。 任何帮助将不胜感激:)

/* 
* Homework 04 Problem 05 
* Student: Kevin Crespin 
* CIN: 
* Description: This program does the following: 
* Ask the user which plan they have. 
* Ask the user for the month number (1-12) of which month they are being billed. 
* Ask the user how many hours they used. 
* Display the cost of their bill. 
* Also display how much money Package 1 customers would would have saved if they purchased 
* packages 2 or 3, and how much money Package 2 customers would have saved if they purchased 
* Package 3. If there were no savings, do not display this message. 
*/ 
// import scanner class 
import java.util.Scanner; 

public class HW04P05{ 
public static void main (String[] args) { 
    Scanner input = new Scanner(System.in); 
    // USER MENU 
    final String USER_MENU = 
     " \n" +\ 
     "[1] Package 1: $15.95 a month for up to 10 hours of service. Additional hours are $2.00 per hour.\n" + 
     "[2] Package 2: $20.95 a month for up to 20 hours of service. Additional hours are $1.00 per hour.\n" + 
     "[3] Package 3: $30.99 per month unlimited access.\n" + 
     " \n" + 
     "Enter [1 - 3] for select your package: "; 
    final int PACKAGE_1 = 1; 
    final int PACKAGE_2 = 2; 
    final int PACKAGE_3 = 3; 
    System.out.print(USER_MENU); 
    int choice = input.nextInt(); 
    System.out.println(" "); 
    if (choice < 0) { 
     System.out.println("The menu choice cannot be negative, must be a value [1 - 3]"); 
     System.out.println("The program will know exit."); 
     System.exit(1); 
    } 
    else if (choice == 0) { 
     System.out.println("The menu choice cannot be zero, must be a value [1 - 3]"); 
     System.out.println("The program will know exit."); 
     System.exit(1); 
    } 
    else if (choice > 3) { 
     System.out.println("The menu choice must be a value [1 - 3]"); 
     System.out.println("The program will know exit."); 
     System.exit(1); 
    } 
    System.out.print("[1] January [4] April [7] July  [10] October\n"); 
    System.out.print("[2] February [5] May [8] August  [11] November\n"); 
    System.out.print("[3] March  [6] June [9] September [12] December\n"); 
    System.out.println(" "); 
    System.out.print("Enter [1 - 12] for select billed month: "); 
    int month = input.nextInt(); 
    if (month < 0) { 
     System.out.println("The month cannot be negative, must be a value [1 - 12]"); 
     System.out.println("The program will know exit."); 
     System.exit(1); 
    } 
    else if (month == 0) { 
    System.out.println("The month cannot be zero, must be a value [1 - 12]"); 
    System.out.println("The program will know exit."); 
    System.exit(1); 
    } 
    else if (month > 12) { 
     System.out.println("The month must be a value [1 - 12]"); 
     System.out.println("The program will know exit."); 
     System.exit(1); 
    } 
    double hours; 
    double savings_1; 
    double savings_2; 
    double total_1; 
    double total_2; 
    final double total_1d = 15.95; 
    final double total_2d = 20.95; 
    final double total_3d = 30.99; 
    // USER MENU SWITCH 
    switch (choice) { 
     case PACKAGE_1: 
      if ((month == 1)^(month == 3)^(month == 5)^(month == 7)^(month == 8)^(month == 10)^(month == 12)) { 
       System.out.print("Enter the number of hours the plan package was used: "); 
       hours = input.nextInt(); 
       if (hours > 744) { 
        System.out.println("ERROR: The number of hours cannot be higher than 744 on the month selected."); 
        System.out.println("Now the program will exit."); 
        System.exit(1); 
       } 
       else if (hours < 0) { 
        System.out.println("ERROR: The number of hours cannot be negative."); 
        System.out.println("Now the program will exit."); 
        System.exit(1); 
       } 
       else { 
        if (hours < 10 && hours >= 1){ 
         System.out.println("The cost of your bill is: $15.95"); 
        } 
        else if (hours > 10 && hours <= 744) { 
         total_1 = ((hours - 10) * 2) + 15.95; 
         System.out.println("The cost of your bill is: $" + total_1); 
        } 
       } 
      } 
      else if (month == 2) { 
       System.out.print("Enter the number of hours the plan package was used: "); 
       hours = input.nextInt(); 
       if (hours > 672) { 
        System.out.println("ERROR: The number of hours cannot be higher than 672 on February."); 
        System.out.println("Now the program will exit."); 
        System.exit(1); 
       } 
       else if (hours < 0) { 
        System.out.println("ERROR: The number of hours cannot be negative."); 
        System.out.println("Now the program will exit."); 
        System.exit(1); 
       } 
       else { 
        if (hours < 10 && hours >= 1){ 
         System.out.println("The cost of your bill is: $15.95"); 
        } 
        else if (hours > 10 && hours <= 672) { 
         total_1 = ((hours - 10) * 2) + 15.95; 
         System.out.println("The cost of your bill is: $" + total_1); 
        } 
       } 
      } 
      else { 
       System.out.print("Enter the number of hours the plan package was used: "); 
       hours = input.nextInt(); 
       if (hours > 720) { 
        System.out.println("ERROR: The number of hours cannot be higher than 722 on the month selected."); 
        System.out.println("Now the program will exit."); 
        System.exit(1); 
       } 
       else if (hours < 0) { 
        System.out.println("ERROR: The number of hours cannot be negative."); 
        System.out.println("Now the program will exit."); 
        System.exit(1); 
      } 
       else { 
        if (hours < 10 && hours >= 1){ 
         System.out.println("The cost of your bill is: $15.95"); 
        } 
        else if (hours > 10 && hours <= 720) { 
         total_1 = ((hours - 10) * 2) + 15.95; 
         System.out.println("The cost of your bill is: $" + total_1); 
        } 
       } 
      } 
     case PACKAGE_2: 
      if ((month == 1)^(month == 3)^(month == 5)^(month == 7)^(month == 8)^(month == 10)^(month == 12)) { 
       if (choice == 2) { 
       System.out.print("Enter the number of hours the plan package was used: "); 
       hours = input.nextInt(); 
       if (hours > 744) { 
        System.out.println("ERROR: The number of hours cannot be higher than 744 on the selected month."); 
        System.out.println("Now the program will exit."); 
        System.exit(1); 
       } 
       else if (hours < 0) { 
        System.out.println("ERROR: The number of hours cannot be negative."); 
        System.out.println("Now the program will exit."); 
        System.exit(1); 
       } 
       else { 
        if (hours < 20 && hours >= 1) { 
         System.out.println("The cost of your bill is: $20.95"); 
        } 
        else if (hours > 20 && hours <= 744) { 
         total_2 = (hours - 20) + 20.95; 
         System.out.println("The cost of your bill is: $" + total_2); 
        } 
       } 
       } 
       else { 
        if (hours < 20 && hours >= 1) { 
         System.out.println("The cost of package 2 bill is: $20.95"); 
         savings_1 = total_1 - total_2d; 
         System.out.println("You could saved: $" + savings_1); 
        } 
        else if (hours > 20 && hours <= 744) { 
         total_2 = (hours - 20) + 20.95; 
         System.out.println("The cost of package 2 bill is: $" + total_2); 
         savings_1 = total_1 - total_2d; 
         System.out.println("You could saved: $" + savings_1); 
        } 
       } 

      else if (month == 2) { 
       if (choice == 2) { 
       System.out.print("Enter the number of hours the plan package was used: "); 
       hours = input.nextInt(); 
       if (hours > 672) { 
        System.out.println("ERROR: The number of hours cannot be higher than 672 on February."); 
        System.out.println("Now the program will exit."); 
        System.exit(1); 
       } 
       else if (hours < 0) { 
        System.out.println("ERROR: The number of hours cannot be negative."); 
        System.out.println("Now the program will exit."); 
        System.exit(1); 
       } 
       else { 
        if (hours < 20 && hours >= 1){ 
         System.out.println("The cost of your bill is: $20.95"); 
        } 
        else if (hours > 20 && hours <= 672) { 
         total_2 = (hours - 20) + 20.95; 
         System.out.println("The cost of your bill is: $" + total_2); 
        } 
       } 
       } 
       else { 
        if (hours < 20 && hours >= 1) { 
         System.out.println("The cost of package 2 bill is: $20.95"); 
         savings_1 = total_1 - total_2d; 
         System.out.println("You could saved: $" + savings_1); 
        } 
        else if (hours > 20 && hours <= 672) { 
         total_2 = (hours - 20) + 20.95; 
         System.out.println("The cost of package 2 bill is: $" + total_2); 
         savings_1 = total_1 - total_2d; 
         System.out.println("You could saved: $" + savings_1); 
        } 
       } 
      } 
      else { 
       if (choice == 2) { 
       System.out.print("Enter the number of hours the plan package was used: "); 
       hours = input.nextInt(); 
       if (hours > 720) { 
        System.out.println("ERROR: The number of hours cannot be higher than 722 on the selected month."); 
        System.out.println("Now the program will exit."); 
        System.exit(1); 
      } 
       else if (hours < 0) { 
        System.out.println("ERROR: The number of hours cannot be negative."); 
        System.out.println("Now the program will exit."); 
        System.exit(1); 
       } 
       else { 
        if (hours < 20 && hours >= 1){ 
         System.out.println("The cost of your bill is: $20.95"); 
        } 
        else if (hours > 20 && hours <= 720) { 
         total_2 = (hours - 20) + 20.95; 
         System.out.println("The cost of your bill is: $" + total_2); 
        } 
       } 
       } 
       else { 
        if (hours < 20 && hours >= 1) { 
         System.out.println("The cost of package 2 bill is: $20.95"); 
         savings_1 = total_1 - total_2d; 
         System.out.println("You could saved: $" + savings_1); 
        } 
        else if (hours > 20 && hours <= 720) { 
         total_2 = (hours - 20) + 20.95; 
         System.out.println("The cost of package 2 bill is: $" + total_2); 
         savings_1 = total_1 - total_2d; 
         System.out.println("You could saved: $" + savings_1); 
        } 
       } 
      } 
     case PACKAGE_3: 
      if ((month == 1)^(month == 3)^(month == 5)^(month == 7)^(month == 8)^(month == 10)^(month == 12)) { 
       if (choice == 3) { 
       System.out.print("Enter the number of hours you used your plan package: "); 
      hours = input.nextInt(); 
       if (hours > 744) { 
        System.out.println("Enter the number of hours the plan package was used: "); 
        System.out.println("Now the program will exit."); 
        System.exit(1); 
       } 
       else if (hours < 0) { 
        System.out.println("ERROR: The number of hours cannot be negative."); 
        System.out.println("Now the program will exit."); 
        System.exit(1); 
       } 
       else { 
        System.out.println("The cost of your bill is: $30.99"); 
       } 
       } 
       else { 
        System.out.println("The cost of package 3 bill is: $30.99"); 
        savings_2 = total_1 - total_3d; 
        System.out.println("You could saved: $" + savings_2); 
       } 
      } 
      else if (month == 2) { 
       if (choice == 3) { 
       System.out.print("Enter the number of hours the plan package was used: "); 
       hours = input.nextInt(); 
       if (hours > 672) { 
        System.out.println("ERROR: The number of hours cannot be higher than 672 on February."); 
        System.out.println("Now the program will exit."); 
        System.exit(1); 
       } 
       else if (hours < 0) { 
        System.out.println("ERROR: The number of hours cannot be negative."); 
        System.out.println("Now the program will exit."); 
        System.exit(1); 
       } 
       else { 
        System.out.println("The cost of your bill is: $30.99"); 
       } 
       } 
       else { 
        System.out.println("The cost of package 3 bill is: $30.99"); 
        savings_2 = total_1 - total_3d; 
        System.out.println("You could saved: $" + savings_2); 
       } 
      }  
      else { 
       if (choice == 3) { 
       System.out.print("Enter the number of hours the plan package was used: "); 
       hours = input.nextInt(); 
       if (hours > 720) { 
        System.out.println("ERROR: The number of hours cannot be higher than 720 on the selected month."); 
        System.out.println("Now the program will exit."); 
        System.exit(1); 
       } 
       else if (hours < 0) { 
        System.out.println("ERROR: The number of hours cannot be negative."); 
        System.out.println("Now the program will exit."); 
        System.exit(1); 
       } 
       else { 
        System.out.println("The cost of your bill is: $30.99"); 
       } 
       } 
       else { 
        System.out.println("The cost of package 3 bill is: $30.99"); 
        savings_2 = total_1 - total_3d; 
        System.out.println("You could saved: $" + savings_2); 
       } 
      } 
    } 
} 
} 

编译代码产生这样的:

HW04P05.java:176: error: variable hours might not have been initialized 
             if (hours < 20 && hours >= 1) { 
              ^
HW04P05.java:178: error: variable total_1 might not have been initialized 
               savings_1 = total_1 - total_2d; 
                  ^
HW04P05.java:184: error: variable total_1 might not have been initialized 
               savings_1 = total_1 - total_2d; 
                  ^
HW04P05.java:214: error: variable hours might not have been initialized 
             if (hours < 20 && hours >= 1) { 
              ^
HW04P05.java:216: error: variable total_1 might not have been initialized 
               savings_1 = total_1 - total_2d; 
                  ^
HW04P05.java:222: error: variable total_1 might not have been initialized 
               savings_1 = total_1 - total_2d; 
                  ^
HW04P05.java:252: error: variable hours might not have been initialized 
             if (hours < 20 && hours >= 1) { 
              ^
HW04P05.java:254: error: variable total_1 might not have been initialized 
               savings_1 = total_1 - total_2d; 
                  ^
HW04P05.java:260: error: variable total_1 might not have been initialized 
               savings_1 = total_1 - total_2d; 
                  ^
HW04P05.java:286: error: variable total_1 might not have been initialized 
             savings_2 = total_1 - total_3d; 
                ^
HW04P05.java:310: error: variable total_1 might not have been initialized 
             savings_2 = total_1 - total_3d; 
                ^
HW04P05.java:334: error: variable total_1 might not have been initialized 
            savings_2 = total_1 - total_3d; 
               ^
12 errors 
+0

你赋值给变量'hours'? –

+0

@IlyaBursov有几行将值分配给小时,但也有一些路径没有。 – Jason

+0

在您的'else-if'语句中的某处,您正在使用变量/ s,而没有为其赋值。 – DigitalNinja

回答

0

你所得到的错误的原因是,在你的代码中的一些执行路径没有一个值设置为hours 。很难看到这个问题,因为您将输入验证与结果计算逻辑混合在一起。

您可以将小时初始化为0,但我怀疑您的程序仍然无法按照您的预期工作。

虽然有很多方法可以解决这个问题,但我建议将代码分解成若干个较小的部分来分别解决每个问题。这有很多优点,包括能够分别测试每件作品,并使其更容易理解,更重要的是,保持每件作品。

如果你把每一块到它自己的方法分离出来,你的主要方法可以是这个样子:

public static void main(String[] args) { 
    int plan = getPlanFromUser(); 
    int month = getMonthFromUser(); 
    int hours = getHoursFromUser(month); 
    evaluateSavings(plan, month, hours); 
} 

然后从用户那里获取计划,你写了一个名为getPlanFromUser(方法)检索用户的输入并在返回有效选择之前对其进行验证。

与其他“获取”方法类似,特殊情况下为getHoursFromUser(),您可以通过月份,以便该方法可以验证是否输入了正确的小时数。

最后,您的evaluateSavings()方法接收所有用户输入(已经过验证)并简单计算出适当的节省。

采取这种方法意味着你只需要在每种方法中解决几个问题,而不必同时考虑所有问题。

编辑:下面是以上

import java.util.Scanner; 

public class HW04P05 { 

    private static final int PACKAGE_1 = 1; 
    private static final int PACKAGE_2 = 2; 
    private static final int PACKAGE_3 = 3; 

    public static void main(String[] args) { 
     Scanner input = new Scanner(System.in); 

     int plan = getPlan(input); 
     int month = getMonth(input); 
     int hours = getHours(input, month); 
     calculateSavings(plan, hours); 
    } 

    private static int getPlan(Scanner input) { 
     // packages menu 
     System.out.println(); 
     System.out.println("[1] Package 1: $15.95 a month for up to 10 hours of service. Additional hours are $2.00 per hour."); 
     System.out.println("[2] Package 2: $20.95 a month for up to 20 hours of service. Additional hours are $1.00 per hour."); 
     System.out.println("[3] Package 3: $30.99 per month unlimited access."); 
     System.out.println(); 
     System.out.print("Enter [1 - 3] for select your package: "); 

     // get input 
     int choice = input.nextInt(); 

     // input validation 
     System.out.println(" "); 
     if (choice < 0) { 
      exitProgram("The menu choice cannot be negative, must be a value [1 - 3]"); 
     } else if (choice == 0) { 
      exitProgram("The menu choice cannot be zero, must be a value [1 - 3]"); 
     } else if (choice > 3) { 
      exitProgram("The menu choice must be a value [1 - 3]"); 
     } 
     return choice; 
    } 

    private static int getMonth(Scanner input) { 
     // month menu 
     System.out.println("[1] January [4] April [7] July  [10] October"); 
     System.out.println("[2] February [5] May [8] August  [11] November"); 
     System.out.println("[3] March  [6] June [9] September [12] December"); 
     System.out.println(""); 
     System.out.print("Enter [1 - 12] for select billed month: "); 

     // get input 
     int month = input.nextInt(); 

     // input validation 
     if (month < 0) { 
      exitProgram("The month cannot be negative, must be a value [1 - 12]"); 
     } else if (month == 0) { 
      exitProgram("The month cannot be zero, must be a value [1 - 12]"); 
     } else if (month > 12) { 
      exitProgram("The month must be a value [1 - 12]"); 
     } 
     return month; 
    } 

    private static int getHours(Scanner input, int month) { 
     // hours menu 
     System.out.println(" "); 
     System.out.print("Enter the number of hours the plan package was used: "); 

     // get input 
     int hours = input.nextInt(); 

     // input validation 
     if (hours < 0) { 
      exitProgram("ERROR: The number of hours cannot be negative."); 
     } 
     if ((month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) && hours > 744) { 
      exitProgram("ERROR: The number of hours cannot be higher than 744 on the month selected."); 
     } 
     if ((month == 4 || month == 6 || month == 9 || month == 11) && hours > 720) { 
      exitProgram("ERROR: The number of hours cannot be higher than 720 on the month selected."); 
     } 
     if (month == 2 && hours > 672) { 
      exitProgram("ERROR: The number of hours cannot be higher than 672 on the month selected."); 
     } 

     return hours; 
    } 

    private static void calculateSavings(int plan, int hours) { 
     double pricePlan1 = calculatePricePlan1(hours); 
     double pricePlan2 = calculatePricePlan2(hours); 
     double pricePlan3 = 30.99; 

     switch(plan) { 
      case PACKAGE_1: 
       System.out.println(String.format("The cost of your bill is: $%.2f", pricePlan1)); 
       if (pricePlan1 > pricePlan2) { 
        System.out.println(String.format("On package 2 you could have saved $%.2f", pricePlan1 - pricePlan2)); 
       } 
       if (pricePlan1 > pricePlan3) { 
        System.out.println(String.format("On package 3 you could have saved $%.2f", pricePlan1 - pricePlan3)); 
       } 
       break; 

      case PACKAGE_2: 
       System.out.println(String.format("The cost of your bill is: $%.2f", pricePlan2)); 
       if (pricePlan2 > pricePlan3) { 
        System.out.println(String.format("On package 3 you could have saved $%.2f", pricePlan2 - pricePlan3)); 
       } 
       break; 

      case PACKAGE_3: 
       System.out.println(String.format("The cost of your bill is: $%.2f", pricePlan3)); 
       break; 
     } 
    } 

    private static double calculatePricePlan1(int hours) { 
     if (hours <= 10) { 
      return 15.95; 
     } 

     return 15.95 + ((hours - 10) * 2); 
    } 


    private static double calculatePricePlan2(int hours) { 
     if (hours <= 20) { 
      return 20.95; 
     } 

     return 20.95 + (hours - 20); 
    } 

    private static void exitProgram(String reason) { 
     System.out.println(reason); 
     System.out.println("The program will now exit."); 
     System.exit(1); 
    } 
} 

注意,每个方法适合于一个“页面”,并且每个可以理解和独立测试如所描述的重组的代码。这种方法使其他开发人员更容易理解代码的功能(从而降低维护成本)。

+0

谢谢! 我能够运行我的代码。但是,我没有使用你的建议,因为我还没有熟悉这个逻辑。正如我所说这是我在Java中的第四周编码,但是,我会在下一个代码中记住它。 –

0

我重新编码并更改了一些部分,我能够按照问题说明来运行代码。

import java.util.Scanner; 
 

 
public class HW04P05{ 
 
    public static void main (String[] args) { 
 
    \t // create scanner 
 
\t Scanner input = new Scanner(System.in); 
 
\t // packages menu 
 
\t final String USER_MENU = 
 
\t \t " \n" + 
 
\t \t "[1] Package 1: $15.95 a month for up to 10 hours of service. Additional hours are $2.00 per hour.\n" + 
 
\t \t "[2] Package 2: $20.95 a month for up to 20 hours of service. Additional hours are $1.00 per hour.\n" + 
 
\t \t "[3] Package 3: $30.99 per month unlimited access.\n" + 
 
\t \t " \n" + 
 
\t \t "Enter [1 - 3] for select your package: "; 
 
\t final int PACKAGE_1 = 1; 
 
\t final int PACKAGE_2 = 2; 
 
\t final int PACKAGE_3 = 3; 
 
\t System.out.print(USER_MENU); 
 
\t int choice = input.nextInt(); 
 
\t // input validation 
 
\t System.out.println(" "); 
 
\t if (choice < 0) { 
 
\t \t System.out.println("The menu choice cannot be negative, must be a value [1 - 3]"); 
 
\t \t System.out.println("The program will know exit."); 
 
\t \t System.exit(1); 
 
\t } 
 
\t else if (choice == 0) { 
 
\t \t System.out.println("The menu choice cannot be zero, must be a value [1 - 3]"); 
 
\t \t System.out.println("The program will know exit."); 
 
\t \t System.exit(1); 
 
\t } 
 
\t else if (choice > 3) { 
 
\t \t System.out.println("The menu choice must be a value [1 - 3]"); 
 
\t \t System.out.println("The program will know exit."); 
 
\t \t System.exit(1); 
 
\t } 
 
\t // month menu 
 
\t System.out.print("[1] January [4] April [7] July  [10] October\n"); 
 
\t System.out.print("[2] February [5] May [8] August  [11] November\n"); 
 
\t System.out.print("[3] March  [6] June [9] September [12] December\n"); 
 
\t System.out.println(" "); 
 
\t System.out.print("Enter [1 - 12] for select billed month: "); 
 
\t int month = input.nextInt(); 
 
\t // input validation 
 
\t if (month < 0) { 
 
\t \t System.out.println("The month cannot be negative, must be a value [1 - 12]"); 
 
\t \t System.out.println("The program will know exit."); 
 
\t \t System.exit(1); 
 
\t } 
 
\t else if (month == 0) { 
 
\t System.out.println("The month cannot be zero, must be a value [1 - 12]"); 
 
\t System.out.println("The program will know exit."); 
 
\t System.exit(1); 
 
\t } 
 
\t else if (month > 12) { 
 
\t \t System.out.println("The month must be a value [1 - 12]"); 
 
\t \t System.out.println("The program will know exit."); 
 
\t \t System.exit(1); 
 
\t } 
 
\t // declaring variables 
 
\t double hours; 
 
\t double savings_1; 
 
\t double savings_2; 
 
\t double total_1; 
 
\t double total_2; 
 
\t final double total_1d = 15.95; 
 
\t final double total_2d = 20.95; 
 
\t final double total_3d = 30.99; 
 
\t // switch statement 
 
\t switch (choice) { 
 
\t \t case PACKAGE_1: 
 
\t \t \t // if statement for 31d, 30d, & 28d months 
 
\t \t \t if ((month == 1)^(month == 3)^(month == 5)^(month == 7)^(month == 8)^(month == 10)^(month == 12)) { 
 
\t \t \t \t System.out.println(" "); 
 
\t \t \t \t System.out.print("Enter the number of hours the plan package was used: "); 
 
\t \t \t \t hours = input.nextInt(); 
 
\t \t \t \t System.out.println(" "); 
 
\t \t \t \t // input validation 
 
\t \t \t \t if (hours > 744) { 
 
\t \t \t \t \t System.out.println("ERROR: The number of hours cannot be higher than 744 on the month selected."); 
 
\t \t \t \t \t System.out.println("Now the program will exit."); 
 
\t \t \t \t \t System.exit(1); 
 
\t \t \t \t } 
 
\t \t \t \t else if (hours < 0) { 
 
\t \t \t \t \t System.out.println("ERROR: The number of hours cannot be negative."); 
 
\t \t \t \t \t System.out.println("Now the program will exit."); 
 
\t \t \t \t \t System.exit(1); 
 
\t \t \t \t } 
 
\t \t \t \t else { 
 
\t \t \t \t \t // computing bill costs and savings 
 
\t \t \t \t \t if (hours < 10 && hours >= 1){ 
 
\t \t \t \t \t \t System.out.println("The cost of your bill is: $15.95"); 
 
\t \t \t \t \t } 
 
\t \t \t \t \t else if (hours > 10 && hours <= 20) { 
 
\t \t \t \t \t \t total_1 = ((hours - 10) * 2) + 15.95; 
 
\t \t \t \t \t \t System.out.println("The cost of your bill is: $" + total_1); 
 
\t \t \t \t \t \t System.out.println(" "); 
 
\t \t \t \t \t \t if (total_1 > 20.95) { 
 
\t \t \t \t \t \t System.out.println("The cost of package 2 bill is: $" + total_2d); 
 
\t \t \t \t \t \t savings_1 = total_2d - total_1; 
 
\t \t \t \t \t \t savings_1 = (savings_1 * -1); 
 
\t \t \t \t \t \t savings_1 = (Math.round(savings_1 * 100)/ 100.0); 
 
\t \t \t \t \t \t System.out.println("You could have saved: $" + savings_1); 
 
\t \t \t \t \t \t System.out.println(" "); 
 
\t \t \t \t \t \t  if (total_1 > 30.99) { 
 
\t \t \t \t \t \t \t System.out.println("The cost of package 3 bill is: $" + total_3d); 
 
\t \t \t \t \t \t \t savings_2 = total_3d - total_1; 
 
\t \t \t \t \t \t \t savings_2 = (savings_2 * -1); 
 
\t \t \t \t \t \t \t savings_2 = (Math.round(savings_2 * 100)/ 100.0); 
 
\t \t \t \t \t \t \t System.out.println("You could have saved: $" + savings_2); 
 
\t \t \t \t \t \t \t System.out.println(" "); 
 
\t \t \t \t \t \t \t } 
 
\t \t \t \t \t  } 
 
\t \t \t \t \t } 
 
\t \t \t \t \t else{ 
 
\t \t \t \t \t \t total_1 = ((hours - 10) * 2) + 15.95; 
 
\t \t \t \t \t \t System.out.println("The cost of your bill is: $" + total_1); 
 
\t \t \t \t \t \t System.out.println(" "); 
 
\t \t \t \t \t \t total_2 = ((hours - 20) + 20.95); 
 
\t \t \t \t \t \t System.out.println("The cost of package 2 bill is: $" + total_2); 
 
\t \t \t \t \t \t savings_1 = total_1 - total_2; 
 
\t \t \t \t \t \t savings_1 = (Math.round(savings_1 * 100)/ 100.0); 
 
\t \t \t \t \t \t System.out.println("You could have saved: $" + savings_1); 
 
\t \t \t \t \t \t System.out.println(" "); 
 
\t \t \t \t \t \t System.out.println("The cost of package 3 bill is: $30.99"); 
 
\t \t \t \t \t \t savings_2 = total_1 - total_3d; 
 
\t \t \t \t \t \t savings_2 = (Math.round(savings_2 * 100)/ 100.0); 
 
\t \t \t \t \t \t System.out.println("You could have saved: $" + savings_2); 
 
\t \t \t \t \t } 
 
\t \t \t \t } 
 
\t \t \t } 
 
\t \t \t else if (month == 2) { 
 
\t \t \t \t System.out.println(" "); 
 
\t \t \t \t System.out.print("Enter the number of hours the plan package was used: "); 
 
\t \t \t \t hours = input.nextInt(); 
 
\t \t \t \t System.out.println(" "); 
 
\t \t \t \t // input validation 
 
\t \t \t \t if (hours > 627) { 
 
\t \t \t \t \t System.out.println("ERROR: The number of hours cannot be higher than 672 on the month selected."); 
 
\t \t \t \t \t System.out.println("Now the program will exit."); 
 
\t \t \t \t \t System.exit(1); 
 
\t \t \t \t } 
 
\t \t \t \t else if (hours < 0) { 
 
\t \t \t \t \t System.out.println("ERROR: The number of hours cannot be negative."); 
 
\t \t \t \t \t System.out.println("Now the program will exit."); 
 
\t \t \t \t \t System.exit(1); 
 
\t \t \t \t } 
 
\t \t \t \t else { 
 
\t \t \t \t \t // computing bill costs and savings 
 
\t \t \t \t \t if (hours < 10 && hours >= 1){ 
 
\t \t \t \t \t \t System.out.println("The cost of your bill is: $15.95"); 
 
\t \t \t \t \t } 
 
\t \t \t \t \t else if (hours > 10 && hours <= 20) { 
 
\t \t \t \t \t \t total_1 = ((hours - 10) * 2) + 15.95; 
 
\t \t \t \t \t \t System.out.println("The cost of your bill is: $" + total_1); 
 
\t \t \t \t \t \t System.out.println(" "); 
 
\t \t \t \t \t \t if (total_1 > 20.95) { 
 
\t \t \t \t \t \t System.out.println("The cost of package 2 bill is: $" + total_2d); 
 
\t \t \t \t \t \t savings_1 = total_2d - total_1; 
 
\t \t \t \t \t \t savings_1 = (savings_1 * -1); 
 
\t \t \t \t \t \t savings_1 = (Math.round(savings_1 * 100)/ 100.0); 
 
\t \t \t \t \t \t System.out.println("You could have saved: $" + savings_1); 
 
\t \t \t \t \t \t System.out.println(" "); 
 
\t \t \t \t \t \t  if (total_1 > 30.99) { 
 
\t \t \t \t \t \t \t System.out.println("The cost of package 3 bill is: $" + total_3d); 
 
\t \t \t \t \t \t \t savings_2 = total_3d - total_1; 
 
\t \t \t \t \t \t \t savings_2 = (savings_2 * -1); 
 
\t \t \t \t \t \t \t savings_2 = (Math.round(savings_2 * 100)/ 100.0); 
 
\t \t \t \t \t \t \t System.out.println("You could have saved: $" + savings_2); 
 
\t \t \t \t \t \t \t System.out.println(" "); 
 
\t \t \t \t \t \t \t } 
 
\t \t \t \t \t \t } 
 
\t \t \t \t \t } 
 
\t \t \t \t \t else{ 
 
\t \t \t \t \t \t total_1 = ((hours - 10) * 2) + 15.95; 
 
\t \t \t \t \t \t System.out.println("The cost of your bill is: $" + total_1); 
 
\t \t \t \t \t \t System.out.println(" "); 
 
\t \t \t \t \t \t total_2 = ((hours - 20) + 20.95); 
 
\t \t \t \t \t \t System.out.println("The cost of package 2 bill is: $" + total_2); 
 
\t \t \t \t \t \t savings_1 = total_1 - total_2; 
 
\t \t \t \t \t \t savings_1 = (Math.round(savings_1 * 100)/ 100.0); 
 
\t \t \t \t \t \t System.out.println("You could have saved: $" + savings_1); 
 
\t \t \t \t \t \t System.out.println(" "); 
 
\t \t \t \t \t \t System.out.println("The cost of package 3 bill is: $30.99"); 
 
\t \t \t \t \t \t savings_2 = total_1 - total_3d; 
 
\t \t \t \t \t \t savings_2 = (Math.round(savings_2 * 100)/ 100.0); 
 
\t \t \t \t \t \t System.out.println("You could have saved: $" + savings_2); 
 
\t \t \t \t \t } 
 
\t \t \t \t } 
 
\t \t \t } 
 
\t \t \t else { 
 
\t \t \t \t System.out.println(" "); 
 
\t \t \t \t System.out.print("Enter the number of hours the plan package was used: "); 
 
\t \t \t \t hours = input.nextInt(); 
 
\t \t \t \t System.out.println(" "); 
 
\t \t \t \t // input validation 
 
\t \t \t \t if (hours > 720) { 
 
\t \t \t \t \t System.out.println("ERROR: The number of hours cannot be higher than 720 on the month selected."); 
 
\t \t \t \t \t System.out.println("Now the program will exit."); 
 
\t \t \t \t \t System.exit(1); 
 
\t \t \t \t } 
 
\t \t \t \t else if (hours < 0) { 
 
\t \t \t \t \t System.out.println("ERROR: The number of hours cannot be negative."); 
 
\t \t \t \t \t System.out.println("Now the program will exit."); 
 
\t \t \t \t \t System.exit(1); 
 
\t \t \t \t } 
 
\t \t \t \t else { 
 
\t \t \t \t \t // computing bill costs and savings 
 
\t \t \t \t \t if (hours < 10 && hours >= 1){ 
 
\t \t \t \t \t \t System.out.println("The cost of your bill is: $15.95"); 
 
\t \t \t \t \t } 
 
\t \t \t \t \t else if (hours > 10 && hours <= 20) { 
 
\t \t \t \t \t \t total_1 = ((hours - 10) * 2) + 15.95; 
 
\t \t \t \t \t \t System.out.println("The cost of your bill is: $" + total_1); 
 
\t \t \t \t \t \t System.out.println(" "); 
 
\t \t \t \t \t \t if (total_1 > 20.95) { 
 
\t \t \t \t \t \t System.out.println("The cost of package 2 bill is: $" + total_2d); 
 
\t \t \t \t \t \t savings_1 = total_2d - total_1; 
 
\t \t \t \t \t \t savings_1 = (savings_1 * -1); 
 
\t \t \t \t \t \t savings_1 = (Math.round(savings_1 * 100)/ 100.0); 
 
\t \t \t \t \t \t System.out.println("You could have saved: $" + savings_1); 
 
\t \t \t \t \t \t System.out.println(" "); 
 
\t \t \t \t \t \t  if (total_1 > 30.99) { 
 
\t \t \t \t \t \t \t System.out.println("The cost of package 3 bill is: $" + total_3d); 
 
\t \t \t \t \t \t \t savings_2 = total_3d - total_1; 
 
\t \t \t \t \t \t \t savings_2 = (savings_2 * -1); 
 
\t \t \t \t \t \t \t savings_2 = (Math.round(savings_2 * 100)/ 100.0); 
 
\t \t \t \t \t \t \t System.out.println("You could have saved: $" + savings_2); 
 
\t \t \t \t \t \t \t System.out.println(" "); 
 
\t \t \t \t \t \t \t } 
 
\t \t \t \t \t  } 
 
\t \t \t \t \t } 
 
\t \t \t \t \t else{ 
 
\t \t \t \t \t \t total_1 = ((hours - 10) * 2) + 15.95; 
 
\t \t \t \t \t \t System.out.println("The cost of your bill is: $" + total_1); 
 
\t \t \t \t \t \t System.out.println(" "); 
 
\t \t \t \t \t \t total_2 = ((hours - 20) + 20.95); 
 
\t \t \t \t \t \t System.out.println("The cost of package 2 bill is: $" + total_2); 
 
\t \t \t \t \t \t savings_1 = total_1 - total_2; 
 
\t \t \t \t \t \t savings_1 = (Math.round(savings_1 * 100)/ 100.0); 
 
\t \t \t \t \t \t System.out.println("You could have saved: $" + savings_1); 
 
\t \t \t \t \t \t System.out.println(" "); 
 
\t \t \t \t \t \t System.out.println("The cost of package 3 bill is: $30.99"); 
 
\t \t \t \t \t \t savings_2 = total_1 - total_3d; 
 
\t \t \t \t \t \t savings_2 = (Math.round(savings_2 * 100)/ 100.0); 
 
\t \t \t \t \t \t System.out.println("You could have saved: $" + savings_2); 
 
\t \t \t \t \t } 
 
\t \t \t \t } 
 
\t \t \t } 
 
\t \t break; \t 
 
\t \t case PACKAGE_2: 
 
\t \t \t if ((month == 1)^(month == 3)^(month == 5)^(month == 7)^(month == 8)^(month == 10)^(month == 12)) { 
 
\t \t \t \t System.out.println(" "); 
 
\t \t \t \t System.out.print("Enter the number of hours the plan package was used: "); 
 
\t \t \t \t hours = input.nextInt(); 
 
\t \t \t \t System.out.println(" "); 
 
\t \t \t \t // input validation 
 
\t \t \t \t if (hours > 744) { 
 
\t \t \t \t \t System.out.println("ERROR: The number of hours cannot be higher than 744 on the month selected."); 
 
\t \t \t \t \t System.out.println("Now the program will exit."); 
 
\t \t \t \t \t System.exit(1); 
 
\t \t \t \t } 
 
\t \t \t \t else if (hours < 0) { 
 
\t \t \t \t \t System.out.println("ERROR: The number of hours cannot be negative."); 
 
\t \t \t \t \t System.out.println("Now the program will exit."); 
 
\t \t \t \t \t System.exit(1); 
 
\t \t \t \t } 
 
\t \t \t \t else { 
 
\t \t \t \t \t // computing bill costs and savings 
 
\t \t \t \t \t if (hours < 20 && hours >= 1){ 
 
\t \t \t \t \t \t System.out.println("The cost of your bill is: $20.95"); 
 
\t \t \t \t \t } 
 
\t \t \t \t \t else { 
 
\t \t \t \t \t \t total_2 = ((hours - 10) * 2) + 15.95; 
 
\t \t \t \t \t \t System.out.println("The cost of your bill is: $" + total_2); 
 
\t \t \t \t \t \t System.out.println(" "); 
 
\t \t \t \t \t \t if (total_2 > 30.99) { 
 
\t \t \t \t \t \t System.out.println("The cost of package 3 bill is: $" + total_3d); 
 
\t \t \t \t \t \t savings_2 = total_3d - total_2; 
 
\t \t \t \t \t \t savings_2 = (savings_2 * -1); 
 
\t \t \t \t \t \t savings_2 = (Math.round(savings_2 * 100)/ 100.0); 
 
\t \t \t \t \t \t System.out.println("You could have saved: $" + savings_2); 
 
\t \t \t \t \t \t System.out.println(" "); 
 
\t \t \t \t \t \t } 
 
\t \t \t \t \t } 
 
\t \t \t \t } \t \t \t 
 
\t \t \t } \t 
 
\t \t \t else if (month == 2) { 
 
\t \t \t \t System.out.println(" "); 
 
\t \t \t \t System.out.print("Enter the number of hours the plan package was used: "); 
 
\t \t \t \t hours = input.nextInt(); 
 
\t \t \t \t System.out.println(" "); 
 
\t \t \t \t // input validation 
 
\t \t \t \t if (hours > 672) { 
 
\t \t \t \t \t System.out.println("ERROR: The number of hours cannot be higher than 672 on the month selected."); 
 
\t \t \t \t \t System.out.println("Now the program will exit."); 
 
\t \t \t \t \t System.exit(1); 
 
\t \t \t \t } 
 
\t \t \t \t else if (hours < 0) { 
 
\t \t \t \t \t System.out.println("ERROR: The number of hours cannot be negative."); 
 
\t \t \t \t \t System.out.println("Now the program will exit."); 
 
\t \t \t \t \t System.exit(1); 
 
\t \t \t \t } 
 
\t \t \t \t else { 
 
\t \t \t \t \t // computing bill costs and savings 
 
\t \t \t \t \t if (hours < 20 && hours >= 1){ 
 
\t \t \t \t \t \t System.out.println("The cost of your bill is: $20.95"); 
 
\t \t \t \t \t } 
 
\t \t \t \t \t else { 
 
\t \t \t \t \t \t total_2 = ((hours - 10) * 2) + 15.95; 
 
\t \t \t \t \t \t System.out.println("The cost of your bill is: $" + total_2); 
 
\t \t \t \t \t \t System.out.println(" "); 
 
\t \t \t \t \t \t if (total_2 > 30.99) { 
 
\t \t \t \t \t \t System.out.println("The cost of package 3 bill is: $" + total_3d); 
 
\t \t \t \t \t \t savings_2 = total_3d - total_2; 
 
\t \t \t \t \t \t savings_2 = (savings_2 * -1); 
 
\t \t \t \t \t \t savings_2 = (Math.round(savings_2 * 100)/ 100.0); 
 
\t \t \t \t \t \t System.out.println("You could have saved: $" + savings_2); 
 
\t \t \t \t \t \t System.out.println(" "); 
 
\t \t \t \t \t \t } 
 
\t \t \t \t \t } 
 
\t \t \t \t } 
 
\t \t \t } 
 
\t \t \t else { 
 
\t \t \t \t System.out.println(" "); 
 
\t \t \t \t System.out.print("Enter the number of hours the plan package was used: "); 
 
\t \t \t \t hours = input.nextInt(); 
 
\t \t \t \t System.out.println(" "); 
 
\t \t \t \t if (hours > 720) { 
 
\t \t \t \t \t System.out.println("ERROR: The number of hours cannot be higher than 720 on the month selected."); 
 
\t \t \t \t \t System.out.println("Now the program will exit."); 
 
\t \t \t \t \t System.exit(1); 
 
\t \t \t \t } 
 
\t \t \t \t else if (hours < 0) { 
 
\t \t \t \t \t System.out.println("ERROR: The number of hours cannot be negative."); 
 
\t \t \t \t \t System.out.println("Now the program will exit."); 
 
\t \t \t \t \t System.exit(1); 
 
\t \t \t \t } 
 
\t \t \t \t else { 
 
\t \t \t \t \t if (hours < 20 && hours >= 1){ 
 
\t \t \t \t \t \t System.out.println("The cost of your bill is: $20.95"); 
 
\t \t \t \t \t } 
 
\t \t \t \t \t else { 
 
\t \t \t \t \t \t total_2 = ((hours - 10) * 2) + 15.95; 
 
\t \t \t \t \t \t System.out.println("The cost of your bill is: $" + total_2); 
 
\t \t \t \t \t \t System.out.println(" "); 
 
\t \t \t \t \t \t if (total_2 > 30.99) { 
 
\t \t \t \t \t \t System.out.println("The cost of package 3 bill is: $" + total_3d); 
 
\t \t \t \t \t \t savings_2 = total_3d - total_2; 
 
\t \t \t \t \t \t savings_2 = (savings_2 * -1); 
 
\t \t \t \t \t \t savings_2 = (Math.round(savings_2 * 100)/ 100.0); 
 
\t \t \t \t \t \t System.out.println("You could have saved: $" + savings_2); 
 
\t \t \t \t \t \t System.out.println(" "); 
 
\t \t \t \t \t \t } 
 
\t \t \t \t \t } 
 
\t \t \t \t } 
 
\t \t \t } 
 
\t \t break; 
 
\t \t case PACKAGE_3: 
 
\t \t \t if ((month == 1)^(month == 3)^(month == 5)^(month == 7)^(month == 8)^(month == 10)^(month == 12)) { 
 
\t \t \t \t System.out.println(" "); 
 
\t \t \t \t System.out.print("Enter the number of hours the plan package was used: "); 
 
\t \t \t \t hours = input.nextInt(); 
 
\t \t \t \t System.out.println(" "); 
 
\t \t \t \t //input validation 
 
\t \t \t \t if (hours > 744) { 
 
\t \t \t \t \t System.out.println("ERROR the number of hours cannot be higher than 744 on the selected month."); 
 
\t \t \t \t \t System.out.println("Now the program will exit."); 
 
\t \t \t \t \t System.exit(1); 
 
\t \t \t \t } 
 
\t \t \t \t else if (hours < 0) { 
 
\t \t \t \t \t System.out.println("ERROR: The number of hours cannot be negative."); 
 
\t \t \t \t \t System.out.println("Now the program will exit."); 
 
\t \t \t \t \t System.exit(1); 
 
\t \t \t \t } 
 
\t \t \t \t else { 
 
\t \t \t \t \t System.out.println("The cost of your bill is: $30.99"); 
 
\t \t \t \t } 
 
\t \t \t } 
 
\t \t \t else if (month == 2) { 
 
\t \t \t \t System.out.println(" "); 
 
\t \t \t \t System.out.print("Enter the number of hours the plan package was used: "); 
 
\t \t \t \t hours = input.nextInt(); 
 
\t \t \t \t System.out.println(" "); 
 
\t \t \t \t // input validation 
 
\t \t \t \t if (hours > 672) { 
 
\t \t \t \t \t System.out.println("ERROR: The number of hours cannot be higher than 672 on the selected month."); 
 
\t \t \t \t \t System.out.println("Now the program will exit."); 
 
\t \t \t \t \t System.exit(1); 
 
\t \t \t \t } 
 
\t \t \t \t else if (hours < 0) { 
 
\t \t \t \t \t System.out.println("ERROR: The number of hours cannot be negative."); 
 
\t \t \t \t \t System.out.println("Now the program will exit."); 
 
\t \t \t \t \t System.exit(1); 
 
\t \t \t \t } 
 
\t \t \t \t else { 
 
\t \t \t \t \t System.out.println("The cost of your bill is: $30.99"); 
 
\t \t \t \t } 
 
\t \t \t } \t 
 
\t \t \t else { 
 
\t \t \t \t System.out.println(" "); 
 
\t \t \t \t System.out.print("Enter the number of hours the plan package was used: "); 
 
\t \t \t \t hours = input.nextInt(); 
 
\t \t \t \t System.out.println(" "); 
 
\t \t \t \t // input validation 
 
\t \t \t \t if (hours > 720) { 
 
\t \t \t \t \t System.out.println("ERROR: The number of hours cannot be higher than 720 on the selected month."); 
 
\t \t \t \t \t System.out.println("Now the program will exit."); 
 
\t \t \t \t \t System.exit(1); 
 
\t \t \t \t } 
 
\t \t \t \t else if (hours < 0) { 
 
\t \t \t \t \t System.out.println("ERROR: The number of hours cannot be negative."); 
 
\t \t \t \t \t System.out.println("Now the program will exit."); 
 
\t \t \t \t \t System.exit(1); 
 
\t \t \t \t } 
 
\t \t \t \t else { 
 
\t \t \t \t \t System.out.println("The cost of your bill is: $30.99"); 
 
\t \t \t \t } 
 
\t \t \t } 
 
\t } 
 
} 
 
}

在哪一行