私は 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