Question
Assignment: Enhance your CinemaPrice assignment from last week by adding a while loop that continues to prompt the user for their age (instead of randomly
Assignment:
Enhance your CinemaPrice assignment from last week by adding a while loop that continues to prompt the user for their age (instead of randomly generating as in the previous version) until a zero is entered.
This is called using a sentinel value (0).
Example output:
Please enter the cinema ticket price: 15.00 Please enter age (0 to quit): 10 Your age : 10 Your ticket price is $7.50 which is half price. Please enter age (0 to quit): 24 Your age : 24 Your ticket price is $15.00 which is full price. Please enter age (0 to quit): 0 Good bye
My Script so far:
import java.text.NumberFormat;
import java.util.Random;
import java.util.Scanner;
public class CinemaPriceEnhanced {
public static void main(String[] args) {
Random gen = new Random( ); //Creates a random generator
int age = gen.nextInt(100) +1; // Random generator between the #1-100
NumberFormat nFmt = NumberFormat.getCurrencyInstance(); // Returns a currency format
Scanner scan = new Scanner(System.in);
System.out.println("Welcome to the Cinema Price Calculator");
System.out.println("--------------------------------------");
System.out.println("Please enter the full cinema ticket price: "); // User puts in a ticket price
double price = scan.nextDouble();
System.out.println("Please enter age (0 to quit): " + age);
System.out.println("Your age: ");
age = scan.nextInt();
while (age <5) // Generates ticket price based on random age
{
price = 0.0;
System.out.println("Your ticket price is $0.00 which is free.");
age++;
}
if (age >=5 && age <=13)
{
System.out.println("Your ticket price is " + nFmt.format(price/2) + " which is half price.");
age++;
}
else if (age >=14 && age <60)
{
System.out.println("Your ticket price is " + nFmt.format(price) + " which is full price.");
age++;
}
else
{
System.out.println("Your ticket price is " + nFmt.format(price = price - 1.10) + " which is the senior discount price.");
age++;
}
System.out.println("Good bye");
scan.close();
}
}
My Output:
Welcome to the Cinema Price Calculator
--------------------------------------
Please enter the full cinema ticket price:
12
Please enter age (0 to quit): 58
Your age:
58
Your ticket price is $12.00 which is full price.
Good bye
My Question is, how do I get it to continue looping until it gets to zero? Thanks.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started