为什么不执行planATotalPrice?

时间:2018-10-05 00:41:12

标签: java

import java.util.Scanner;//Needed for the Scanner class

/ **   *程序要求用户输入客户购买的包裹的字母(A,B,C)   *和使用的小时数。   *然后程序将计算客户的每月账单。   *套餐A:每月9.95美元,包括10个小时的互联网。额外的时间为每小时2美元。   *套餐B:每月13.95美元,包括20小时的互联网服务。额外的小时数为每小时1美元。   *套餐AC:每月19.95美元,提供无限制的Internet访问。   *   *该程序计算并显示套餐A客户在以下情况下可以节省的金额:   *他们购买了套餐B或套餐C,如果   *他们购买了C。   * /

公共类InternetSavingsCh3 {   public static void main(String [] args)// main方法开始执行Java应用程序   {       字符串输入; //保留用户的输入

  double planAFixedPrice = 9.95;
  double planBFixedPrice = 13.95;
  double planCFixedPrice = 19.95; //  to hold the final monthly bill
  double planAIncludedHours = 10.00; // total hours for interner plan
  double plaBIncludedHours = 20.00; // total hours for interner plan
  double planCIncludedHours = 0.0; // total hours for interner plan
  double planAExtraHours,planBExtraHours, planCExtraHours;
  double planAPricePerExtraHour = 2.0;  //  to hold the amount of charged for additional hours of internet use
  double planBPricePerExtraHour = 1.0;   //  to hold the amount of charged for additional hours of internet use
  double planCPricePerExtraHour = 0.0; //  to hold the amount of charged for additional hours of internet use
  double planASavingsOnPlanB, planASavingsOnPlanC, planBSavingsOnPlanC;//  to hold the amount of savings per package
  double planATotalPrice, planBTotalPrice, planCTotalPrice;
  double totalHours; // to hold the total hours the Internet was used 

  Scanner keyboard = new Scanner(System.in); //create an object to read input from the standard input channel System.in (typically, the keyboard)

  System.out.println(" Enter the letter of the Internet package you have purchased A,B,or C: "); // prompt the user for the internet package
  input = keyboard.nextLine();

  char packageInternet = input.charAt(0); // Internet package selected by the user

  System.out.println(" Enter the total number of hours you used the Internet this month: "); //display instructions
  totalHours = keyboard.nextDouble();

  switch(packageInternet) // use switch to read 'a' and 'A' input
  {
      case 'a':
      case 'A':
        System.out.println(" Plan A cost $9.95 per month and 10 hours of Internet are included. "
        +"Additional hours are $1 per hour. "); //display message to the user
        planAExtraHours = totalHours - planAIncludedHours;
        planATotalPrice = planAFixedPrice + (planAExtraHours * planAPricePerExtraHour); ; //calculate plan A customer total bill for the month
        System.out.println(" Your Internet bill for this month is: $"+ planATotalPrice); //display message to the user

        planASavingsOnPlanB = planATotalPrice - planBFixedPrice; //compute the amount saved if purchasing plan B instead of plan A
        planASavingsOnPlanC = planATotalPrice - planCFixedPrice; //compute the amount saved if purchasing plan C instead of plan A
        System.out.println(" On plan B, you would have saved: $"+ planASavingsOnPlanB + ". "); //display the message to the user
        System.out.println(" On plan C, you would have saved: $"+ planASavingsOnPlanC + ". ") ; //display the message to the user
        break;

      case 'b':
      case 'B':
        System.out.println(" Plan B cost $13.95 per month and 20 hours of Internet are included. "
        +"Additional hours are $2 per hour. "); //display message to the user
        planBExtraHours = totalHours - plaBIncludedHours;
        planBTotalPrice = planBFixedPrice + (planBExtraHours * planBPricePerExtraHour); //calculate plan B customer total bill for the month
        System.out.println(" Your Internet bill for this month is: $"+ planBTotalPrice); //display message to the user
        planBSavingsOnPlanC = planBTotalPrice - planCFixedPrice; // compute the savings for for Package C customer
        System.out.println(" On plan C, you would have saved: $" + planBSavingsOnPlanC + ". "); //display the message
        break;

      case 'c':
      case 'C':
        System.out.println(" Plan C cost $19.95 per month with unlimited Internet access included. "); //display message to the user
        System.out.println(" Your Internet bill for this month is: $" + planCFixedPrice); //display message to the user
        break;

      default:
        System.out.println(" Invalid choice. Please enter the internet package letter. "); //display message to the user
        break;

  }   
 // End the program
  System.exit(0);

}

2 个答案:

答案 0 :(得分:0)

您有重复的变量名,因此出现错误。声明后,无需使用double a = ..;

再次声明

此外,您不能在初始化变量之前使用它。仅仅声明变量是行不通的。

您可以使用以下内容作为参考,并根据需要进行逻辑处理:

package edu.ucla.loni.ida.handle.ajax.search;

import java.util.Scanner;//Needed for the Scanner class

/**
* The program asks the user to enter the letter of the package the customer has purchased (A,B,C)
* and the number of hours that were used. 
* The program will then calculate the customer's monthly bill.
* Package A: $9.95 per month 10 hours of Internet included. Additional hours are $2 per hour.
* Package B: $13.95 per month 20 hours of Internet included. Additional hours are $1 per hour.
* Package AC: $19.95 per month unlimited Internet access provided.
* 
* The program calculates and display the amount of money Package A customers would save if 
* they purchased Package B or C, and the amount of money Package B customers would save if 
* they purchased C.
*/

public class InternetSavingsCh3 {
    public static void main(String[] args) // main method begins execution of Java application
    {
        String input; // to hold the user's input

        double planAFixedPrice = 9.95;
        double planBFixedPrice = 13.95;
        double planCFixedPrice = 19.95; // to hold the final monthly bill

        double planAIncludedHours = 0.0;
        double planBIncludedHours = 0.0;
        double planCIncludedHours = 0.0; // to enter the total hours for interner use
        double planAPricePerExtraHour = 2;
        double planBPricePerExtraHour = 0.0; 
        double planCPricePerExtraHour = 0.0; // to hold the amount of charged for additional hours of internet use
        double planASavingsOnPlanB, planASavingsOnPlanC, planBSavingOnPlanC;// to hold the amount of savings per package
        double totalHours; // to hold the total hours the Internet was used

        Scanner keyboard = new Scanner(System.in); // create an object to read input from the standard input channel System.in (typically, the keyboard)

        System.out.println(
                " Enter the letter of the Internet package you have purchased A,B,or C: "); // prompt the user for the internet package
        input = keyboard.nextLine();

        char packageInternet = input.charAt(0); // Internet package selected by the user

        System.out.println(
                " Enter the total number of hours you used the Internet this month: "); // display instructions
        totalHours = keyboard.nextDouble();

        switch (packageInternet) // use switch to read 'a' and 'A' input
        {
        case 'a':
        case 'A':
            System.out.println(
                    " Plan A cost $9.95 per month and 10 hours of Internet are included. "
                            + "Additional hours are $1 per hour. "); // display message to the user
            double planAExtraHours = totalHours - planAIncludedHours;
            double planATotalPrice = planAFixedPrice
                    + planAExtraHours * planAPricePerExtraHour;
            ; // calculate plan A customer total bill for the month
            System.out.println(
                    " Your Internet bill for this month is: $" + planATotalPrice); // display message to the user

            planASavingsOnPlanB = planATotalPrice - planBFixedPrice; // compute the amount saved if purchasing plan B instead of plan A
            planASavingsOnPlanC = planATotalPrice - planCFixedPrice; // compute the amount saved if purchasing plan C instead of plan A
            System.out.println(
                    " On plan B, you would have saved: $" + planASavingsOnPlanB + ". "); // display the message to the user
            System.out.println(
                    " On plan C, you would have saved: $" + planASavingsOnPlanC + ". "); // display the message to the user
            break;

        case 'b':
        case 'B':
            System.out.println(
                    " Plan B cost $13.95 per month and 20 hours of Internet are included. "
                            + "Additional hours are $2 per hour. "); // display message to the user
            double planBExtraHours = totalHours - planBIncludedHours;
            double planBTotalPrice = planBFixedPrice
                    + planBExtraHours * planBPricePerExtraHour; // calculate plan B customer total bill for the month
            System.out.println(
                    " Your Internet bill for this month is: $" + planBTotalPrice); // display message to the user
            planBSavingOnPlanC = planBTotalPrice - planCFixedPrice; // compute the savings for for Package C customer
            System.out.println(
                    " On plan C, you would have saved: $" + planBSavingOnPlanC + ". "); // display the message
            break;

        case 'c':
        case 'C':
            System.out.println(
                    " Plan C cost $19.95 per month with unlimited Internet access included. "); // display message to the user
            System.out.println(
                    " Your Internet bill for this month is: $" + planCFixedPrice); // display message to the user
            break;

        default:
            System.out.println(
                    " Invalid choice. Please enter the internet package letter. "); // display message to the user
            break;

        }
        // End the program
        System.exit(0);

    }
}

答案 1 :(得分:0)

之所以会这样,是因为您已经声明了2次变量,并且作用域重叠(overlap)……在这种情况下,您只声明了一次:

    double planASavingsOnPlanB, planASavingsOnPlanC, planBSavingOnPlanC;//  to hold the amount of savings per package

      double planASavingsOnPlanB = planATotalPrice - planBFixedPrice; //compute the amount saved if purchasing plan B instead of plan A

基本上,计算机不知道它将/应该使用哪一台计算机。