Nice start. You are on your way there. I had only a few minor changes to this and added a few things. Plus I left a little for you to do. Read the comments to see what I have done here.
CODE
import java.util.Random;
import java.text.DecimalFormat;
public class CinemaPrice
{
private Random generator;
public static void main (String[] args)
{
// Variables to hold age and price of person
int age = 0;
double priceNumber = 0.00;
// Generate a random age (generator will
Random generator = new Random();
age = generator.nextInt(100) + 1;
// Check age of person and assign pricing.
// Notice how I combine both age 5 with 55 or over.
// I am saying here "if age is 5 or less OR age is 55 and over, assign no price"
if ((age <= 5) || (age >= 55)) {
priceNumber = 0.0;
}else if (age <= 12){
priceNumber = 6.25;
}else if (age <= 54){
priceNumber = 12.50;
}else {
System.out.println("Sorry, but the age supplied was invalid.");
}
// Check if they are free, print message to say so.
// Otherwise print the person's age and the price.
if (priceNumber <= 0.0) {
System.out.println("The person age " + age + " is free!");
}
else {
System.out.println("Price for the person age " + age + " is: $" + priceNumber);
}
}
}
Some of the things you will notice is the change of a few of your variables here. Whenever you need to store a decimal value, you must use the appropriate data type. In this case double. An
int data type would cut off the decimal portion of the number which is not what you want. Also note I got rid of your string variables here. You didn't need them to just include them in the message at the bottom. Notice how I concatenated them together. Lastly, notice that if the age didn't meet our requirements I couldn't assign the value "invalid" to our price because it is a string. You should only assign strings to a string variable, not the double we are now using or the integer you were using before.
Please note that I didn't format the price for you. But I did give you the tools to do it. Notice I included the class
DecimalFormat in there for you. Use this class and its method
format() to format the price into an appropriate money value. I will let you look that up and figure out how to do it. Hint: you will need to instantiate an instance of a DecimalFormat object first and pay attention to how you can instantiate it!
Enjoy!