vendredi 26 juin 2020

If else statement inside boolean while loop not completing. Java [duplicate]

I am currently working on a project which involves purchasing tickets for a basketball game. I have collapsed everything except the documentation section and the method causing issues.

import java.util.Scanner;

public class Project2_Part1 {
    static double ticketPrice = 80;
    static double studentDisc = .5;
    static double staffDisc = .2;
    static double militaryDisc = .15;
    static double alumniDisc = .1;
    static double totalPrice;

    public static void main(String[] args) {
        printMenu();
        selection();
        purchaseInfo();

    }

    static void printMenu() 
    {
        
    }

    private static void selection() 
    {
        
    }

    private static void purchaseInfo()
    {
        boolean isAnswerAccepted;
        isAnswerAccepted = true;
        int numTickets;
        String parkingPass;
        
        System.out.println("How many tickets would you like to purchase?");
        Scanner num = new Scanner(System.in);
        numTickets = num.nextInt();
        
        totalPrice = ticketPrice * numTickets;
        System.out.println("Would you like to purchase a parking pass?");
        parkingPass = num.next();
        while(isAnswerAccepted) {
            
            if(parkingPass == "Y" || parkingPass == "y") 
            {
                totalPrice = totalPrice + 25;
                System.out.println("you ordered " + numTickets + " tickets with parking for a total cost of " + totalPrice);
                isAnswerAccepted = false;
            }
            else if(parkingPass == "N" || parkingPass == "n")
            {
                System.out.println("you ordered " + numTickets + " tickets without parking for a total cost of " + totalPrice);
                isAnswerAccepted = false;
            }
            else 
            {
                
            }
        }       
    }
}

Back when the code was different i had boolean isAnswerAccepted at the top, but that caused me to get static reference errors, and this wasn't meant to be a static bool. So from what i understand, i need to keep the boolean down there.

Currently, when i run the code it works just fine until the i am asked if i would like a parking pass. whatever i type in, it ends the code and doesnt continue.

Welcome to Basketball!
Here you'll be able to get your tickets for the upcoming Tournament!

What category of seats would you like to purchase for the tournament?

1) Student
2) Alumni
3) Military
4) Faculty
5) General Public
1
How many tickets would you like to purchase?
2
Would you like to purchase a parking pass?
y

it stops right here and i am not entirely sure why. I believe it has something to do with my boolean values but im not sure what exactly is wrong with it, since the print command comes before i set the boolean value to false. If anyone can help i would greatly appreciate it. Thank you.

Aucun commentaire:

Enregistrer un commentaire