我有一个正在上学的作业。我几乎已经完成了99%的工作,但是我为最后一件事而苦苦挣扎。
最后一个开关选项需要进行计算,只要使用它的人按下“ 1”键即可。如果没有,它将给出一条消息,提示密钥错误。
我的问题是它可以毫无问题地进行计算,但是即使有人按下“ 1”以外的任何东西,它仍然可以完成计算。
我想念什么?
int option, quantity;
float childTotal=0;
final double childCost=18.00;
float adultTotal=0;
final double adultCost=36.00;
float seniorTotal=0;
final double seniorCost=32.50;
Scanner input = new Scanner(System.in); //InputStream initialisation
System.out.println("@@@@@ Welcome to Zoo Victoria @@@@@");
System.out.println();
System.out.println("MAIN MENU");
System.out.println();
System.out.println("Please choose from the following ticket type");
System.out.println();
System.out.println("1 = Child (4-5 years)");
System.out.println("2 = Adult (18+ years)");
System.out.println("3 = Senior (60+ years)");
System.out.println();
System.out.println("Enter your ticket type");
option=input.nextInt();
System.out.println("Enter the total number of tickets to be purchased");
quantity=input.nextInt();
System.out.print("You wish to purchase " +(quantity));
switch (option) {
case 1: System.out.println (" child tickets at $18.00");
break;
case 2: System.out.println (" adult tickets at $36.00");
break;
case 3: System.out.println (" senior tickets at $32.50");
break;
}
System.out.println("Press 1 to confirm purchase");
option=input.nextInt();
{
switch (option) {
case 1: childTotal=(float) (childTotal + (childCost * quantity));
System.out.println(childTotal);
break;
case 2: adultTotal=(float) (adultTotal + (adultCost * quantity));
System.out.println(adultTotal);
break;
case 3: seniorTotal=(float) (seniorTotal + (seniorCost * quantity));
System.out.println(seniorTotal);
break;
default:
System.out.println("Incorrect Key");
}
}
}
}
答案 0 :(得分:0)
我相信,原因是您对两个不同的操作使用了相同的变量。 就像这里:
您重新初始化了选项变量,然后在打算使用第一个选项= input.nextInt()时再次使用它
option = input.nextInt();
switch (option) {
case 1:
childTotal = (float) (childTotal + (childCost * quantity));
System.out.println(childTotal);
break;
case 2:
adultTotal = (float) (adultTotal + (adultCost * quantity));
System.out.println(adultTotal);
break;
case 3:
seniorTotal = (float) (seniorTotal + (seniorCost * quantity));
System.out.println(seniorTotal);
break;
default:
System.out.println("Incorrect Key");
}
可能的解决方案:
System.out.println("Press 1 to confirm purchase");
if(input.nextInt() == 1){
switch (option) {
case 1:
childTotal = (float) (childTotal + (childCost * quantity));
System.out.println(childTotal);
break;
case 2:
adultTotal = (float) (adultTotal + (adultCost * quantity));
System.out.println(adultTotal);
break;
case 3:
seniorTotal = (float) (seniorTotal + (seniorCost * quantity));
System.out.println(seniorTotal);
break;
default:
System.out.println("Incorrect Key");
}
}else{
System.out.println("err");
// do what you want
}
答案 1 :(得分:0)
第二个switch语句中的逻辑不正确。考虑这个例子,假设我购买了一张高级票(在您的情况下为3张),并且在确认(按1)后,控制权转到case 1
,即为孩子计算账单。因此,我们得到的输出不是$32.50
,而不是$18.00
。
您可以简单地要求用户使用相同的选项来确认购买:
System.out.println("Please choose from the following to confirm purchase");
System.out.println("1 = Child (4-5 years)\n2 = Adult (18+ years)\n3 = Senior (60+ years)");
option = input.nextInt();
switch (option) {
case 1:
childTotal = (float) (childTotal + (childCost * quantity));
System.out.println(childTotal);
break;
case 2:
adultTotal = (float) (adultTotal + (adultCost * quantity));
System.out.println(adultTotal);
break;
case 3:
seniorTotal = (float) (seniorTotal + (seniorCost * quantity));
System.out.println(seniorTotal);
break;
default:
System.out.println("Incorrect Key");
}