Java 为什么不执行planATotalPrice?
/** *程序要求用户输入客户购买的软件包的字母(A、B、C) *以及使用的小时数。 *然后,该程序将计算客户的每月账单。 *套餐A:每月9.95美元,含10小时上网时间。额外工时为每小时2美元。 *套餐B:每月13.95美元,含上网20小时。额外工时为每小时1美元。 *AC套餐:每月19.95美元,不限互联网接入。 * *该程序计算并显示客户在以下情况下可以节省的金额: *他们购买了B包或C包,如果 *他们购买了C。 */ 公共级互联网储蓄学校3 { publicstaticvoidmain(String[]args)//main方法开始执行Java应用程序 { 字符串输入;//保存用户的输入Java 为什么不执行planATotalPrice?,java,Java,/** *程序要求用户输入客户购买的软件包的字母(A、B、C) *以及使用的小时数。 *然后,该程序将计算客户的每月账单。 *套餐A:每月9.95美元,含10小时上网时间。额外工时为每小时2美元。 *套餐B:每月13.95美元,含上网20小时。额外工时为每小时1美元。 *AC套餐:每月19.95美元,不限互联网接入。 * *该程序计算并显示客户在以下情况下可以节省的金额: *他们购买了B包或C包,如果 *他们购买了C。 */ 公共级互联网储蓄学校3 { publicstaticvoidmain
import java.util.Scanner;//Needed for the Scanner class
}您有重复的变量名,因此会出现错误。声明后,无需使用
double a=..;再次声明
此外,在初始化变量之前不能使用它。仅声明该变量是无效的
您可以使用以下内容作为参考,并根据您的要求处理逻辑:
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次,并且范围重叠……在本例中,您声明了一次:
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);
}
}
及
基本上,计算机不知道它会/应该使用哪一个。数学怎么办?有什么问题吗?我无法获得最终结果。具体来说,当用户输入计划A,例如小时数50时,程序停止。为什么?我如何运行程序以获得当月的总成本?你忘了复制计划Ae字符串输入;但代码似乎对我有效。我的结果在下一条评论中。输入您购买的Internet软件包A、B或C的字母:A输入您本月使用Internet的总小时数:50计划A每月花费9.95美元,包括10小时的Internet。额外的小时数为每小时1美元。您的Internet这个月的账单是:在B计划中89.95美元,你会节省76.0美元。在C计划中,你会节省70.0美元。
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