Little help exiting JAVA program.

Java programming topics
Post Reply
User avatar
Trebor29
Lieutenant
Lieutenant
Posts: 75
Joined: Thu Apr 29, 2010 12:34 am

Little help exiting JAVA program.

Post by Trebor29 » Thu Apr 29, 2010 12:51 am

Hi, I think this is an easy one... When option==5 (Quit) the program quits correctly, but it also reads the "Sorry, I do not bla bla..." message. I know it is because it is not recognising 5 as a valid option! I can add an if (option==5) so that it fixes the problem but it still asks user to input first/second numbers before exiting! Little help to take the pressure off is much appreciated. ;)

Code: Select all

import java.util.Scanner;

class methodCalc
{
	public static void menu()
	{
		System.out.println("1) Add");
		System.out.println("2) Subtract");
		System.out.println("3) Multiply");
		System.out.println("4) Divide");
		System.out.println("5) Quit");
	}
	
	public static void main(String[]args)
	{
		Scanner input = new Scanner(System.in);
		
		int option=0;
		
		while (option!=5)
		{
			menu();
			
			System.out.println("\nPlease choose an option from the menu.");
			System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
			option = input.nextInt();
			
			if (option==1 || option==2 || option==3 || option==4)
			{
			
				System.out.println("Please enter your first number.");
				int num1 = input.nextInt();
				System.out.println("Please enter your second number.");
				int num2 = input.nextInt();
				System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
				
				if (option==1)
				{
					int a = Add(num1,num2);
					System.out.print(num1+" + "+num2);
					System.out.print(" = " + a);
					System.out.println("\n================================\n");
				}
				
				if (option==2)
				{
					int a = Sub(num1,num2);
					System.out.print(num1+" - "+num2);
					System.out.print(" = " + a);
					System.out.println("\n================================\n");
				}
				
				if (option==3)
				{
					int a = Multi(num1,num2);
					System.out.print(num1+" * "+num2);
					System.out.print(" = " + a);
					System.out.println("\n================================\n");
				}
				
				if (option==4)
				{
					int a = Div(num1,num2);
					System.out.print(num1+" / "+num2);
					System.out.print(" = " + a);
					System.out.println("\n================================\n");
				}
			}
			else
				System.out.println("\nSorry, I do not recognise that input.\n");
			
		}
		System.out.println("Thank you, come again");
		
	}
	
	public static int Add(int num1,int num2)
	{
		return(num1+num2);
	}
	
	public static int Sub(int num1,int num2)
	{
		return(num1-num2);
	}
	
	public static int Multi(int num1,int num2)
	{
		return(num1*num2);
	}
	
	public static int Div(int num1,int num2)
	{
		return(num1/num2);
	}
}
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: Little help exiting JAVA program.

Post by Neo » Thu Apr 29, 2010 10:17 am

Trebor, my dear friend. How are you?
First of all welcome to ROBOT.LK!!!

How was the previous program. Was that working?

I found a little issue in this one and the corrected code is as below.

Code: Select all

import java.util.Scanner;

class methodCalc
{
   public static void menu()
   {
      System.out.println("1) Add");
      System.out.println("2) Subtract");
      System.out.println("3) Multiply");
      System.out.println("4) Divide");
      System.out.println("5) Quit");
   }
   
   public static void main(String[]args)
   {
      Scanner input = new Scanner(System.in);
      
      int option=0;
      
      while (option!=5)
      {
         menu();
         
         System.out.println("\nPlease choose an option from the menu.");
         System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
         option = input.nextInt();
         
         if (option==1 || option==2 || option==3 || option==4)
         {
         
            System.out.println("Please enter your first number.");
            int num1 = input.nextInt();
            System.out.println("Please enter your second number.");
            int num2 = input.nextInt();
            System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
            
            if (option==1)
            {
               int a = Add(num1,num2);
               System.out.print(num1+" + "+num2);
               System.out.print(" = " + a);
               System.out.println("\n================================\n");
            }
            
            if (option==2)
            {
               int a = Sub(num1,num2);
               System.out.print(num1+" - "+num2);
               System.out.print(" = " + a);
               System.out.println("\n================================\n");
            }
            
            if (option==3)
            {
               int a = Multi(num1,num2);
               System.out.print(num1+" * "+num2);
               System.out.print(" = " + a);
               System.out.println("\n================================\n");
            }
            
            if (option==4)
            {
               int a = Div(num1,num2);
               System.out.print(num1+" / "+num2);
               System.out.print(" = " + a);
               System.out.println("\n================================\n");
            }
         }
         else if (option != 5){
            System.out.println("\nSorry, I do not recognise that input.\n");
         }
         
      }
      System.out.println("Thank you, come again");
      
   }
   
   public static int Add(int num1,int num2)
   {
      return(num1+num2);
   }
   
   public static int Sub(int num1,int num2)
   {
      return(num1-num2);
   }
   
   public static int Multi(int num1,int num2)
   {
      return(num1*num2);
   }
   
   public static int Div(int num1,int num2)
   {
      return(num1/num2);
   }
}
Hope that helps!
User avatar
Trebor29
Lieutenant
Lieutenant
Posts: 75
Joined: Thu Apr 29, 2010 12:34 am

Re: Little help exiting JAVA program.

Post by Trebor29 » Thu Apr 29, 2010 10:41 pm

Hello Neo my old mucka.. lol
Thanks for replying so soon, but Im afraid it is still outputing the "Sorry message" instead of just outputing the "Thank you message" when the user selects 5 to quit the program.
Is this a test to see if I can sort it from here??? or are you losing your touch! :o lolol :D
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: Little help exiting JAVA program.

Post by Neo » Thu Apr 29, 2010 11:25 pm

Ohhh. I was thinking that the Quit menu is 4 (May be I have written a recent program to someone with 4 for Quit ;) ).
I have updated the program in my previous post.

However what you have to do is, replace following code

Code: Select all

         else if (option != 4){
            System.out.println("\nSorry, I do not recognise that input.\n");
         }
with this one.

Code: Select all

         else if (option != 5){
            System.out.println("\nSorry, I do not recognise that input.\n");
         }
User avatar
Trebor29
Lieutenant
Lieutenant
Posts: 75
Joined: Thu Apr 29, 2010 12:34 am

Re: Little help exiting JAVA program.

Post by Trebor29 » Sat May 01, 2010 12:05 am

Thanks Neo, That did cross my mind when I was thinking about it in bed, sometimes I can be abit lazy :roll: that should have jumped out at me! :D
Post Reply

Return to “Java Programming”