This is a JAVA question. How do I preserve my initial price so that subsequent c
ID: 670765 • Letter: T
Question
This is a JAVA question. How do I preserve my initial price so that subsequent calculations in the IF statement always use the initial price? I'd prefer not to make a separate variable if possible. Here is the part of the code that the question refers to.
System.out.println("Please enter the cinema ticket price: ");
price = scan.nextDouble();
System.out.println("Please enter age (0 to quit): ");
age = scan.nextInt();
//Sentinel Value
while (age != 0)
{
//Ticket Price
if (age < 5)
{
price = 0;
System.out.println("Your age: " + age);
System.out.println("Your ticket price is " + cFmt.format(price) + " which is free.");
}
else if (age <= 12)
{
price /= 2;
System.out.println("Your age: " + age);
System.out.println("Your ticket price is " + cFmt.format(price) + " which is half price.");
}
else if (age <= 54)
{
System.out.println("Your age: " + age);
System.out.println("Your ticket price is " + cFmt.format(price) + " which is full price.");
}
else if (age >= 55)
{
price = 0;
System.out.println("Your age: " + age);
System.out.println("Your ticket price is " + cFmt.format(price) + " which is free.");
}
System.out.println(" Please enter age (0 to quit): ");
age = scan.nextInt();
}
Explanation / Answer
Scanner scan=new Scanner(System.in);
System.out.println("Please enter the cinema ticket price: ");
double price = scan.nextDouble();
System.out.println("Please enter age (0 to quit): ");
int age = scan.nextInt();
double temp=age;
while (age != 0)
{
//Ticket Price
if (age < 5)
{
System.out.println("Your age: " + age);
System.out.println("Your ticket price is " + cFmt.format(0) + " which is free.");
}
else if (age <= 12)
{
System.out.println("Your age: " + age);
System.out.println("Your ticket price is " + cFmt.format(price/2) + " which is half price.");
}
else if (age <= 54)
{
System.out.println("Your age: " + age);
System.out.println("Your ticket price is " + cFmt.format(price) + " which is full price.");
}
else if (age >= 55)
{
System.out.println("Your age: " + age);
System.out.println("Your ticket price is " + cFmt.format(0) + " which is free.");
}
System.out.println(" Please enter age (0 to quit): ");
age = scan.nextInt();
with out making it equal to price just send the half value of price
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.