我正在为我的CSCI课程开展一个项目,我已经完成了大部分工作。我的switch语句有问题(第142行到第177行)。如果我运行程序并选择选择4它会给我答案,但它也会显示我的默认值。我需要知道如何让它停止这样做。
import java.util.Scanner;
import java.text.DecimalFormat;
public class Project2
{
public static void clearScreen()
{
System.out.print(
"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" +
"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" +
"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"
);
}// end clearScreen()
public static void displayInfo()
{
System.out.print(
"***** WELCOME TO THE TIP CALCULATOR *****\n" +
"\tcreated by Allen Watson\n\n"
);
}// end displayInfo()
public static void pressEntertoContinue()
{
Scanner Keyboard = new Scanner(System.in);
System.out.print("\t--Press enter to continue--");
Keyboard.nextLine();
}// end pressEntertoContinue()
public static void main(String [] args)
//***************************VARIABLE DECLARATIONS*****************************
//-------------------------------input by user---------------------------------
float fPurchaseAmt;
byte bTipMenuSelection;
float fUserTipPercentage = 0f;
//--------------------------------calculated-----------------------------------
float fTipAmt;
//---------------------------------constants-----------------------------------
final float TIP_PERCENTAGE_1 = 10.0f;
final float TIP_PERCENTAGE_2 = 15.0f;
final float TIP_PERCENTAGE_3 = 20.0f;
final byte ONE_HUNDRED = 100;
//*************************INPUT - PROCESSING - OUTPUT*************************
//-----------------------------------------------------------------------------
Scanner Keyboard = new Scanner(System.in);
clearScreen();
displayInfo();
pressEntertoContinue();
clearScreen();
System.out.print("Enter the amount of purchase: ");
fPurchaseAmt = Keyboard.nextFloat();
System.out.print(
"\n\nPlease make a selection from the menu below" +
"\n\n\tTip Calculator Menu"+
"\n\t-------------------" +
"\n\t1. 10% tip" +
"\n\t2. 15% tip" +
"\n\t3. 20% tip" +
"\n\t4. Enter a tip percentage." +
"\n\n\tEnter your selection: ");
bTipMenuSelection = Keyboard.nextByte();
DecimalFormat dfMoney = new DecimalFormat("$#,##0.00");
switch(bTipMenuSelection)
{
case 1:
fTipAmt = ((TIP_PERCENTAGE_1 / ONE_HUNDRED) * fPurchaseAmt);
System.out.print(
"\nA " + TIP_PERCENTAGE_1 + "% tip for a " +
dfMoney.format(fPurchaseAmt) + " purchase would be " +
dfMoney.format(fTipAmt));
break;
case 2:
fTipAmt = ((TIP_PERCENTAGE_2 / ONE_HUNDRED) * fPurchaseAmt);
System.out.print(
"\nA " + TIP_PERCENTAGE_2 + "% tip for a " +
dfMoney.format(fPurchaseAmt) + " purchase would be " +
dfMoney.format(fTipAmt));
break;
case 3:
fTipAmt = ((TIP_PERCENTAGE_3 / ONE_HUNDRED) * fPurchaseAmt);
System.out.print(
"\nA " + TIP_PERCENTAGE_3 + "% tip for a " +
dfMoney.format(fPurchaseAmt) + " purchase would be " +
dfMoney.format(fTipAmt));
break;
case 4:
System.out.print("\nPlease enter a tip percentage: ");
fUserTipPercentage = Keyboard.nextShort();
fTipAmt = ((fUserTipPercentage / ONE_HUNDRED) * fPurchaseAmt);
System.out.print(
"\nA " + fUserTipPercentage + "% tip for a " +
dfMoney.format(fPurchaseAmt) + " purchase would be " +
dfMoney.format(fTipAmt));
default:
fTipAmt = 0.0f;
System.out.print("\n\tERROR");
break;
}
//-----------------------------------------------------------------------------
}// End of main.
}// End of Project1
答案 0 :(得分:2)
这真的很简单。
因为java遵循(破坏的)C约定,每个case
需要break
或者与下面的代码混淆,所以你需要确保在任何地方放置break
。
你忘了一个,这就是为什么事情搞砸了。
case 4:
System.out.print("\nPlease enter a tip percentage: ");
fUserTipPercentage = Keyboard.nextShort();
fTipAmt = ((fUserTipPercentage / ONE_HUNDRED) * fPurchaseAmt);
System.out.print(
"\nA " + fUserTipPercentage + "% tip for a " +
dfMoney.format(fPurchaseAmt) + " purchase would be " +
dfMoney.format(fTipAmt));
<<----------------------- No break!
default:
fTipAmt = 0.0f; <<----- therefore executing will continue here.
System.out.print("\n\tERROR");
break;
}