How would i fix this issue of getting more than one variable

So im kind of new to coding, im doing some school work where im supposed to make a simple script to calculate the cost of a movie for each person, for example, a senior pays $10, a child pays $9, and others pay $12. However, I keep getting 2 variables at the end of the output. is there anything here that could fix this problem? Heres my code.

import java.util.*;

public class MovieCost
{
  public static void main(String [] args)
  {
     Scanner myInput = new Scanner(System.in);
     String name;//user's name
     int age;//user's age
     
     //Get user's name and age
     System.out.println("What is your name?");
     name = myInput.nextLine();
     System.out.println("How old are you?");
     age = myInput.nextInt();
     if (age>=65)//check if senior ticket
     {
       System.out.println("The movie will cost $10");
     }
     else //if not a senior ticket, check if child ticket
     {
       System.out.println("The movie will cost $12");
     }
       if (age<=12)//check if child ticket
       {
       System.out.println("The movie will cost $9");
     }
     else //if not a child ticket, full adult price
       {
       System.out.println("The movie will cost $12");
     }
     
     }//end main
  }//end class

Welcome to the forums, @krystianzolnierowski. You have posted your question in the JavaScript forum, but you ask about Java. I have moved this thread to a forum where you will be more likely to get a response.

thank you very much, sorry for the confusion.

Here’s where the trouble is occurring.

     if (age>=65)//check if senior ticket
     {
       System.out.println("The movie will cost $10");
     }
     else //if not a senior ticket, check if child ticket
     {
       System.out.println("The movie will cost $12");
     }
       if (age<=12)//check if child ticket
       {
       System.out.println("The movie will cost $9");
     }
     else //if not a child ticket, full adult price
       {
       System.out.println("The movie will cost $12");
     }

I recommend that you check for the things that you’re interested in first, before finally using the else clause for the default situation.

     if (age >= 65) //check if senior ticket
     {
      System.out.println("The movie will cost $10");
     } else if (age <= 12) //check if child ticket
     {
      System.out.println("The movie will cost $9");
     } else // full adult price
     {
      System.out.println("The movie will cost $12");
     }

Thank you so much, it works now :smiley:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.