How to PRINT OUT THE MENU in the code (methods)?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ccarter45
    New Member
    • Mar 2008
    • 10

    How to PRINT OUT THE MENU in the code (methods)?

    What am I missing?
    Code:
    import java.util.Scanner;
      
    public class MathTutor
    {
      public static void main( String[] args ) 
      {  
        int choice;
        
        choice = printMenu();
        while (choice != 4)
        {
          if ( choice == 1 )
            additionTutor();
          else if ( choice == 2 )
            substractionTutor();
          else if ( choice == 3 )
            multiplicationTutor();
          
    
        }
      }
      
      // method prints out the main menu and gets user choice for menu item  
      public static int printMenu()
      {
        System.out.println( "Welcome to Math Tutor!" );
        System.out.println( "1 - Addtion" );
        System.out.println( "2 - Substraction" );
        System.out.println( "3 - Multiplication" );
        System.out.println( "4 - Quit" );
        int choice = 0;
        int i =1;
        boolean exit= true;
    
        
       
      }
  • Stwange
    Recognized Expert New Member
    • Aug 2007
    • 126

    #2
    Try this (if I understood you correctly):

    Code:
    import java.util.Scanner;
    import java.io.*;
      
    public class MathTutor
    {
      public static void main( String[] args ) 
      {  
        int choice;
        
        choice = printMenu();
        while (choice != 4)
        {
          if ( choice == 1 )
            additionTutor();
          else if ( choice == 2 )
            substractionTutor();
          else if ( choice == 3 )
            multiplicationTutor();
          
    
        }
      }
      
      // method prints out the main menu and gets user choice for menu item  
      public static int printMenu()
      {
        System.out.println( "Welcome to Math Tutor!" );
        System.out.println( "1 - Addtion" );
        System.out.println( "2 - Substraction" );
        System.out.println( "3 - Multiplication" );
        System.out.println( "4 - Quit" );
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        try {
    
    	int choice = Integer.parseInt(in.readLine());
            return choice;
        }
        catch (IOException e) {
            /* log it? return -1? whatever you want to do. */
            e.printStackTrace();
            throw new Error("IOException");
       }
     }

    Comment

    Working...