2016-05-03 2 views
1

Так что в моем текущем коде есть проблема, что из подменю, когда я нажимаю exit (4), он не возвращает меня в главное меню, он просто повторяет оператор if внутри основное меню в цикле, которое инициирует подменю. Как это исправить?Возврат в главное меню из подменю в java

import java.util.Scanner; 
import java.util.*; 
import java.util.ArrayList; 

public class StarberksInterface 
{ 


public static void main(String args[]) 
{ 

    Scanner console = new Scanner(System.in); 
    store = new Store(); 
    String str, sName1, sName2, name; 
    char c; 
    int n=0; 
    sName1 = "Callahan"; 
    sName2 = "Lambton"; 


    //This is the main menu that will be displayed first. 
    System.out.println("  MAIN MENU FOR MANAGEMENT SYSTEM"); 
    System.out.println("==============================================="); 
    System.out.println("1. CHOOSE STORE"); 
    System.out.println("2. DISPLAY STORES"); 
    System.out.println("3. LOAD STORE VIA FILE"); 
    System.out.println("4. SAVE STORE TO FILE "); 
    System.out.println("5. EXIT PROGRAM"); 
    System.out.println("==============================================="); 

    while(n!=5)// Exits the program when 4 is pressed 
     { 
      System.out.print("\n Please enter option 1-4 to continue...: "); 
      n = Integer.parseInt(System.console().readLine()); 
      // Reads user input and takes them to selected code. 
      if (n>5||n<1) 
      { 
       System.out.print("Invalid input, please try again..."); 
       continue; 
      } 
      if (n==1)// Takes to option 1 or sub menu 
      { 
       str="y"; 

       while(str.equals("y")||str.equals("Y")) 
       { 
        System.out.println("Enter a store name [Callahan or Lambton] "); 
        name = console.next(); 
        if (sName1.equals(name)|| sName2.equals(name)) 
        { 
         StarberksInterface.subMenu(); 
         return; 
        } 
        else 
        { 
         System.out.println("There is no store under this name. Please try again."); 
        } 

       } 

      } 
      if (n==2)// Gathers products in stores and displays the number of products 
      { 
       System.out.println(" Store data is being displayed."); 
       System.out.println("==============================="); 
       System.out.println("Store: Callahan"); 
       System.out.println("  Number of products: "+store.getProductListSize()); 

      } 
     } 
} 




public static void subMenu() 
{ 
Scanner console = new Scanner(System.in); 
String str; 
char c; 
int n=0; 


     // this will be the sub menu that gets displayed. 
     System.out.println("  INVENTORY MANAGEMENT SYSTEM"); 
     System.out.println("==============================================="); 
     System.out.println("1. ADD PRODUCT DATA"); 
     System.out.println("2. VIEW SINGLE PRODUCT DATA"); 
     System.out.println("3. DELETE PRODUCT"); 
     System.out.println("4. DISPLAY ALL PRODUCTS IN STORE"); 
     System.out.println("==============================================="); 
     System.out.println("5. EXIT SUB MENU"); 

     while(n!=5)// Exits the program when 4 is pressed 
     { 
      System.out.print("\n Please enter option 1-5 to continue...: "); 
      n = Integer.parseInt(System.console().readLine()); 
      // Reads user input and takes them to selected code. 
      if (n>5||n<1) 
      { 
       System.out.print("Invalid input, please try again..."); 
       continue; 
      } 
      if (n==1)// Takes to option 1 or addItem() 
      { 
       str="y"; 
       while(str.equals("y")||str.equals("Y")) 
       { 

        StarberksInterface.addItem(); 
        System.out.print("Would you like to enter another product ? (Y or N) : "); 
        str = console.next(); 
       } 
       continue; 
      } 
      if (n==2)// Takes to option 2 or prodData 
      { 
       str="y"; 
       while(str.equals("y")||str.equals("Y")) 
       { 
        StarberksInterface.prodData(); 
        System.out.println("\n***************************************************\n"); 
        System.out.print("Stay viewing this page? (Y or N) "); 
        str = console.next(); 

       } 
       continue; 
      } 

      if (n==3)// Takes to option 3 or delete item 
      { 
       System.out.print("Delete a product"); 
       continue; 
      } 

      if (n==4)// Takes to option 4 or view all products in store 
      { 
       System.out.print("Displaying all products in store"); 
       continue; 
      } 
     } 
      if (product != null)// If there is information on the system 
      // then the user will have the option to view data, before the program quits 
      { 
       System.out.println("\n***************************************************\n"); 
       System.out.println("\nAre you sure you want to quit? There is information stored on a product. "); 
       System.out.println("\nWould you like to view if information? (Y/N) "); 
       str=""; 
       str = console.next(); 
       while(str.equals("y")||str.equals("Y")) 
       { 

        StarberksInterface.prodData(); 
        return; 
       } 

      } 

      else 
      { 
       System.out.print("\nThank you for using this inventory management software.\n"); 
       System.out.print("Developed by Xavier Edwards"); 
       System.out.println("\n***************************************************\n"); 
      } 

ответ

1

Изменение возвращения заявления в

if (sName1.equals(name) || sName2.equals(name)) { 
    StarberksInterface.subMenu(); 
    return; 
} 

в

if (sName1.equals(name) || sName2.equals(name)) { 
    StarberksInterface.subMenu(); 
    break; 
} 

Чтобы снова Выведите меню вам нужно будет переместить главное меню внутри цикла :

while (n != 5)// Exits the program when 4 is pressed{ 
    ..... 
} 
+0

я попробовал это, и это не работало, до сих пор сделал то же самое, что и раньше –

+0

@ Raznish Smith, изменяя 'return' на' break', работает для m е. –

+0

также вместо 'System.console(). ReadLine()' use 'console.next()' –

0

Вы должны переместить основное меню внутри этого цикла:

while(n!=5)/ 
{ 
... 
} 
+0

извините, но это не сработало:/ –

0

пытаются заменить

n = Integer.parseInt(System.console().readLine()); 

по

n = console.nextInt(); 

в обоих местах

Смежные вопросы