【发布时间】:2018-06-01 20:18:03
【问题描述】:
我对 Java 还是很陌生。我有这个任务,我应该编写一个计算客户每月账单的程序。它应该要求用户输入客户购买的包裹的字母(A、B 或 C)以及使用的分钟数。然后它应该会显示总费用。
规格如下:
互联网服务提供商为其客户提供三种不同的订阅套餐:
套餐 A:每月 39.99 美元,提供 450 分钟的访问权限。额外分钟为每分钟 0.45 美元。套餐 B:每月 59.99 美元,提供 900 分钟的访问权限。额外小时为每分钟 0.40 美元。套餐 C:每月 69.99 美元,提供无限制访问。
这是我的代码:
import java.util.Scanner;
public class assignment2
{
public static void main(String[] args)
{
//declaration section
double AddTime;
double Charges;
double Saving;
double Bill;
int Subscription;
int TimeMin;
Scanner input = new Scanner (System.in);
//ask user subscription
System.out.println("Please enter you subscription (A,B, or C) ");
Subscription = input.nextInt ();
//if user choses subscription A
switch (Subscription)
{
case 'a':
case 'A':
System.out.println("Enter amount of minutes spent this month ");
TimeMin = input.nextInt ();
if (TimeMin < 450)
{
AddTime = TimeMin - 450 ;
Charges = AddTime * 0.45;
System.out.println("The charges are: $" + Charges);
Bill = 39.99 + Charges;
System.out.println("Your bill this month will be: $" + Bill);
System.out.println(" ");
//mention the user the savings with other subscriptions
Saving = Bill - 59.99;
System.out.println("With package B you would have saved: $" + Saving);
Saving = Bill - 69.99;
System.out.println("With package C you would have saved: $" + Saving);
}
else
{System.out.println("Your bill is $39.99 this month ");}
break;
case 'b':
case 'B':
// ask user for the amount of minutes used
System.out.println("Enter amount of minutes spent this month ");
TimeMin = input.nextInt();
if (TimeMin < 900)
{
AddTime = TimeMin - 900;
Charges = AddTime * 0.40;
System.out.println("The charges are: $" + Charges);
Bill = 59.99 + Charges;
System.out.println("Your bill this month will be: $" + Bill);
System.out.println(" ");
//mention the user the savings with other subscriptions
Saving = Bill - 69.99;
System.out.println("With package C you would have saved: $" + Saving);
}
else
{System.out.println("Your bill is $59.99 this month ");}
break;
case 'c':
case 'C':
// ask user for the amount of minutes used
System.out.println("Enter amount of minutes spent this month ");
TimeMin = input.nextInt();
System.out.println("Your bill is $69.99 this month you have unlimited access to our service!");
break;
default:
System.out.println("That is not A, B, or C!");
}
System.out.println("Thanks for using our service!");
}
}
一旦我尝试编译它就会给我这个错误:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at assignment2.main(assignment2.java:21)
我还没有解决。知道如何修复我的代码吗?
【问题讨论】:
-
Javascript 不是 Java。请努力学习您尝试编写代码的语言的名称,并适当地标记您的问题。
-
System.out.println("Please enter you subscription (A,B, or C) ");这看起来需要整数输入吗?如果没有,为什么要有Subscription = input.nextInt ();? -
Subscription = input.nextInt ();表示用户应该键入数字。使用Scanner.next()。 -
附注:此“错误”不是标题所述的编译错误。这是一个运行时异常。由于编译器错误,程序甚至无法运行/编译。我想提到的第二件事是,您应该考虑让自己了解 Java 编码约定,例如 Google java Style Guide,以使您的代码对其他人更具可读性和可维护性。第三点:提问时要保持专业和客观。减少诸如“lol”、“nerds”之类的词。非常感谢。