所以我仍然是Java新手,但对于作业,我需要
这是挑战13
互联网服务提供商 Internet服务提供商为其客户提供三种不同的订阅包: 套餐A:每月9.95美元,提供10小时访问。额外的小时是每小时2.00美元。 套餐B:每月13.95美元,提供20小时访问。额外的时间是每小时1.00美元。 套餐C:每月19.95美元,提供无限制访问。 编写一个计算客户月度账单的程序。它应该要求用户输入客户购买的包裹的字母* A,B或C)以及使用的小时数。然后它应显示总费用。
说明修改您为编程挑战13编写的程序
所以这是我打印的代码:
package assignment14;
import java.util.*;
public class Assignment14
{
public static void main(String[] args)
{
String userInput;
char packageChosen;
double packageA,packageB,totalHours, totalSavingsAtoB, totalSavingsAtoC, totalSavingsBtoC;
/*THE ERROR MESSAGE OCCURS IN THIS LINE FOR THE FIRST packageA AND FOR packageB*/
totalSavingsAtoB = (packageA - 13.95);
totalSavingsAtoC = (packageA - 19.95);
totalSavingsBtoC = (packageB - 19.95);
Scanner keyboard = new Scanner(System.in);
System.out.println("Which package have you obtain? (Please use A, B, or C)");
userInput = keyboard.nextLine();
packageChosen = userInput.charAt(0);
switch (packageChosen)
{
case 'A':
System.out.println("What are the total amount of hours used for this month?");
totalHours = keyboard.nextDouble();
if (totalHours <= 10)
{
packageA = 9.95 * totalHours;
System.out.print("For Package A:\nThe total charges for this month is $ " + packageA);
System.out.print("\nYou save a total of " + totalSavingsAtoB + "if you chose Package B\n"
+ "and you would have save a total of " + totalSavingsAtoC + "if you chose Package C.");
}
else
packageA = 9.95 + (totalHours * 2.00);
System.out.print("For Package A:\nThe total charges for this month is $ " + packageA);
if (totalSavingsAtoB > packageA)
{
totalSavingsAtoB = (packageA - 13.95);
totalSavingsAtoC = (packageA - 19.95);
System.out.print("\nYou save a total of " + totalSavingsAtoB + "if you chose Package B\n"
+ "and you would have save a total of " + totalSavingsAtoC + "if you chose Package C.");
}
break;
case 'B':
System.out.println("What are the total amount of hours used for this month?");
totalHours = keyboard.nextDouble();
if (totalHours <= 20)
{
packageB = 13.95 * totalHours;
System.out.print("For Package B:\nThe total charges for this month is $" + packageB);
System.out.print("You would have save a total of " + totalSavingsBtoC + ", if you have chosen Package C.");
}
else
{
packageB = 13.95 + (totalHours * 1.00);
System.out.print("For Package B:\nThe total charges for this month is $" + packageB);
System.out.print("You would have save a total of " + totalSavingsBtoC + ", if you have chosen Package C.");
}
if (totalSavingsAtoB > packageA)
{
totalSavingsBtoC = (packageB - 19.95);
System.out.print("\nYou save a total of " + totalSavingsAtoB + "if you chose Package B\n"
+ "and you would have save a total of " + totalSavingsAtoC + "if you chose Package C.");
}
break;
case 'C':
System.out.print("For Package C:\nThe total charge for this is $19.95.");
System.out.print(" \nThank you for using our service! " );
break;
default:
System.out.print("Invalid entry, Please try again.");
}
}
}
我一直为packageA和packageB获取的错误消息是这些变量可能尚未初始化。
答案 0 :(得分:1)
在初始化之前使用变量时会发生此错误(就像它说的那样)。编译器未为本地变量(例如您的packageA
或packageB
)分配默认值。
换句话说,它的抱怨是因为它不知道你在行totalSavingsAtoB = (packageA - 13.95)
(以及其他行)中从中减去13.95。
所以,你需要在使用它们之前将packageA
和packageB
设置为某个值(可能是0?也许是包的成本是多少?我不知道。)
<强>除了强>
根据上下文我会猜测那些给你带来麻烦的3行应该在你设置值后去某个地方(我注意到你在交换机的每种情况下分配它们)。