-2

我正在为我的 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
4

1 回答 1

2

这真的很简单。
因为java遵循(破碎的)C约定,每个人都case需要有abreak或与它下面的代码混淆,你需要确保你把breaks放在任何地方。

你忘了一个,这就是为什么事情搞砸了。

   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;
    }
于 2013-10-05T20:34:38.600 回答