JAVA-总计未计算-带If else语句的Switch语句

问题描述 投票:-2回答:1

我正在制定一个跟踪正在运行的志愿者的程序,该程序的目标是跟踪(县/县外)有多少跑步者,并显示每位志愿者的费用,然后得出总计结束。我的switch语句方法(第234行)遇到问题,该方法无法正确返回总计。它显示为$ 0,这显然是不可能的。

public class Runner 

package festival;

import java.util.Scanner;
/**
 * @JacobBarnard CMSY-166-001
 * Assignment: Lab 04
 * Due date: 04/07/2020
 * Program Description: Using a sentinel controlled do while loop and selection 
 * statements to ask the user for data and to display information along with the total costs of their selections 
 */
public class Runner 
{
    // declaring constants for each switch statement
    public final static int FIVEK = 1, TENK = 2, HALF = 3, FULL = 4, FUN = 5;

    // RUNNING COSTS [NON HOWARD COUNTY RESIDENT]
    public final static int NONHC_FIVEK_COST = 40, NONHC_TENK_COST = 50, NONHC_HALF_COST = 85, NONHC_FULL_COST = 115, NONHC_FUN_COST = 25;

    // RUNNING COSTS [HOWARD COUNTY RESIDENT]
    public final static int HC_FIVEK_COST = 25, HC_TENK_COST = 35, HC_HALF_COST = 65, HC_FULL_COST = 85, HC_FUN_COST = 20;

    public static void main(String [] args) 
    {

        Scanner myScanner = new Scanner(System.in);

        // creating/instaniating variables
        int age = 0;
        int countRunner = 0;
        int countHCResidents = 0;
        int countNonHCResidents = 0;
        int numChildren = 0;
        int numAdults = 0;
        int numSeniors = 0;
        int raceChoice = 0;
        int maxChoice = 0;
        int fiveKRunners = 0;
        int tenKRunners = 0;
        int halfMarathonRunners = 0;
        int fullMarathonRunners = 0;
        int funRunRunners = 0;
        double totalCost = 0;
        boolean HC_Resident = false;
        double grandTotal = 0;

        // prompt user if they would like to add a runner and error trap invalid input 
        System.out.println("Would you like to add a runner?");
        String answer =myScanner.nextLine();

        while (! (answer.equalsIgnoreCase("yes") || answer.equalsIgnoreCase("no")))
        {
            System.out.println("Invalid Entry. Only 'yes' or 'no' is acceptable Please reenter:");
            System.out.println("Enter 'yes' or 'no'.");
            answer =myScanner.nextLine();
        } 

        // Check if runner is howard county resident
        while ( answer.equalsIgnoreCase("yes") )
        {
            // *** TAL: Counter for number of runners. See below:
            countRunner++;

            System.out.println("Is the Runner a Howard County resident?");
            String answer1 =myScanner.nextLine();

            while (! (answer1.equalsIgnoreCase("yes") || answer1.equalsIgnoreCase("no")))
            {
                System.out.println("Invalid Entry. Only 'yes' or 'no' is acceptable. Please reenter:");
                System.out.println("Enter 'Yes' or 'No':");
                answer1 =myScanner.nextLine();
            }

            // Adds user to appropriate counter based on y/n
            if (answer1.equalsIgnoreCase("yes"))
            {
                countHCResidents++;
                HC_Resident = true;
            }
            else
            {
                countNonHCResidents++;
                HC_Resident = false;
            }

            System.out.println("What is the runner's age?"); // prompt for age input
            age = myScanner.nextInt();

            while (age <= 0) // error trap for valid input
            {
                System.out.println("Invalid Entry. Age must be greater than 0. Please reenter:");
                System.out.println("What is the runner's age?");
                age = myScanner.nextInt();
            }
            // adding user to the appropriate groups based on age range
            if (age < 10)
            {
                numChildren++;
            }       
            else if (age >= 70) 
            {
                numSeniors++;
            }
            else 
            {
                numAdults++;
            }

            // Check if runner is eligible for the fun run
            boolean eligibleFunRun = (age < 10 || age >= 70);

            // Checking age for out of county discount
            boolean getsDiscount = (age <= 18 || age >= 60);

            // prompt user with running even options
            System.out.println("Select the event the runner will be running in:");
            System.out.println("1 - 5K");
            System.out.println("2 - 10K");
            System.out.println("3 - Half Marathon");
            System.out.println("4 - Full Marathon");

            // display only if user is eligible for the fun run
            if (eligibleFunRun)
            {
                System.out.println("5 - 1 Mile Fun Run");
            }

            // storing users choice in raceChoice
            raceChoice = myScanner.nextInt();

            // error trap
            maxChoice = (eligibleFunRun ? 5 : 4);

            while( (raceChoice < 1) || (raceChoice > maxChoice) )
            {
                // prompt user again with menu
                System.out.println("Invalid Entry.  Please select a value from the menu.  Please reenter: Select the event the runner will be running in:");
                System.out.println("1 - 5K");
                System.out.println("2 - 10K");
                System.out.println("3 - Half Marathon");
                System.out.println("4 - Full Marathon");

                // check if user is eligible for fun run
                if (eligibleFunRun)
                {
                    System.out.println("5 - 1 Mile Fun Run");
                }
                // storing user input in raceChoice
                raceChoice = myScanner.nextInt();               
            }

            // Adding user appropriately to event groups
            if (raceChoice == 1)
            {
                fiveKRunners++;
            }
            else if (raceChoice == 2)
            {
                tenKRunners++;
            }
            else if (raceChoice == 3)
            {
                halfMarathonRunners++;
            }
            else if (raceChoice == 4)
            {
                fullMarathonRunners++;
            }
            else
            {
                funRunRunners++;
            }

            // ----------------------------(CALCULATIONS)---------------------------------------------------------------------------------
            // Calculating cost based on user input and apply necesary discounts
            raceChoiceSwitch(raceChoice, totalCost, getsDiscount, HC_Resident, grandTotal);

            //*** TAL: need to flush the buffer here before accepting new input with the nextLine() method.
            myScanner.nextLine();

            // ** TAL: Logic to prompt and determine if the loop needs to continue or not:
            System.out.println("Would you like to add a runner?");
            answer = myScanner.nextLine();

            while (! (answer.equalsIgnoreCase("yes") || answer.equalsIgnoreCase("no")) )
            {
                System.out.println("Invalid Entry. Only 'yes' or 'no' is acceptable.  Please reenter:");
                System.out.println("Enter 'yes' or 'no'.");
                answer = myScanner.nextLine();
            } 

        } // end main while loop

        // Display total amount of runners
        System.out.println("Total Runners: " + countRunner);
        System.out.println("Total Howard County Residents: " + countHCResidents);
        System.out.println("Total Non-Howard County Residents: " + countNonHCResidents);
        System.out.println("Total number of children: " + numChildren);
        System.out.println("Total number of adults: " + numAdults);
        System.out.println("Total number of seniors: " + numSeniors);
        System.out.println("Total 5K Runners: " + fiveKRunners);
        System.out.println("Total 10K Runners: " + tenKRunners);
        System.out.println("Total Half Marathon Runners: " + halfMarathonRunners);
        System.out.println("Total Full Marathon Runners: " + fullMarathonRunners);
        System.out.println("Total Fun Run Participants: " + funRunRunners);

        displayCost(totalCost); // displaying the total cost of all runners

        // *** TAL: Close the input file stream before execution leaves the main method:
        myScanner.close();



    } // end main method


    // display total cost constructor
    public static void displayCost(double grandTotal) 
    {
        System.out.printf("%s%.2f%n","\nThe total cost of all the races entered is $",grandTotal,"\n");

    }

    public static double calculateDiscount(double totalCost,boolean getDiscount)
    {
        if (getDiscount) // Checking if runner gets the out of county discount
        {
            totalCost -= 5;

        } // end of if statement

        return totalCost;
    } // end of calculate cost

    public static double raceChoiceSwitch(int raceChoice, double totalCost, boolean getsDiscount, boolean HC_Resident, double grandTotal) {
        switch(raceChoice)

        {
            case FIVEK: // calculating cost of 5k

                // Checking if a county resident and applying the appropriate price
                if(HC_Resident == true)                 
                {
                    totalCost = HC_FIVEK_COST;
                    System.out.printf("%s%.2f%n","You have entered the 5K race. The total cost is $",totalCost,"\n");
                    System.out.println();
                    totalCost += grandTotal;
                }
                else
                {
                    totalCost = NONHC_FIVEK_COST;
                    totalCost = calculateDiscount(totalCost, getsDiscount);
                    System.out.printf("%s%.2f%n","You have entered the 5K race. The total cost is $",totalCost,"\n");
                    System.out.println();
                    totalCost += grandTotal;
                } // end of FIVEK cost case
                break;


            case TENK: // calculating cost of 10k

                // Checking if a county resident and applying the appropriate price
                if(HC_Resident = true)                  
                {
                    totalCost += HC_TENK_COST;
                    System.out.printf("%s%.2f%n","You have entered the 10K race. The total cost is $",totalCost,"\n");
                    System.out.println();

                }
                else
                {
                    totalCost = NONHC_TENK_COST;
                    totalCost = calculateDiscount(totalCost, getsDiscount);
                    System.out.printf("%s%.2f%n","You have entered the 10K race. The total cost is $",totalCost,"\n");
                    System.out.println();

                } // end of TENK cost case

                break;

            case HALF: // calculating cost of half marathon

                // Checking if a county resident and applying the appropriate price
                if(HC_Resident == true)                 {
                    totalCost += HC_HALF_COST;
                    System.out.printf("%s%.2f%n","You have entered the Half Marathon race. The total cost is $",totalCost,"\n");
                    System.out.println();

                }
                else
                {
                    totalCost = NONHC_HALF_COST;
                    totalCost = calculateDiscount(totalCost, getsDiscount);
                    System.out.printf("%s%.2f%n","You have entered the Half Marathon race. The total cost is $",totalCost,"\n");
                    System.out.println();

                } // end of HALF marathon cost case

                break;

            case FULL: // calculating cost of full marathon

                // Checking if a county resident and applying the appropriate price
                if(HC_Resident == true)                 {
                    totalCost += HC_FULL_COST;
                    System.out.printf("%s%.2f%n","You have entered the Full Marathon race. The total cost is $",totalCost,"\n");
                    System.out.println();
                }
                else
                {
                    totalCost = NONHC_FULL_COST;
                    totalCost = calculateDiscount(totalCost, getsDiscount);
                    System.out.printf("%s%.2f%n","You have entered the Full Marathon race. The total cost is $",totalCost,"\n");
                    System.out.println();

                } // end of full marathon cost case

                break;

            case FUN: // calculating cost of fun run

                // Checking if a county resident and applying the appropriate price
                if(HC_Resident == true)  {
                    totalCost += HC_FUN_COST;
                    System.out.printf("%s%.2f%n","You have entered the Fun Run race. The total cost is $",totalCost,"\n");
                    System.out.println();
                }
                else
                {
                    totalCost = HC_FUN_COST;
                    totalCost = calculateDiscount(totalCost, getsDiscount);
                    System.out.printf("%s%.2f%n","You have entered the Fun Run race. The total cost is $",totalCost,"\n");
                    System.out.println();
                }

                break;  // end of fun run cost case


        } // end of raceChoice switch

        return grandTotal;

    }


} // end Runner class
java
1个回答
0
投票

您在switch语句中称为totalCost += grandTotal。这会将grandTotal的值(看起来是零)添加到totalCost。我想您打算打grandTotal += totalCost。否则,每当您调用0时,您只需将totalCost += grandTotal添加到totalCost中,因为grandTotal永远不会从其0.]的起始值开始更改

© www.soinside.com 2019 - 2024. All rights reserved.