So hat mein derzeitiger Code das Problem, dass aus dem Untermenü, wenn ich Exit (4) drücken Sie mich nicht zum Hauptmenü zurück, es wiederholt nur die if-Anweisung innerhalb der Hauptmenü While-Schleife, die das Untermenü initiiert. Wie behebe ich das?Zurück zum Hauptmenü aus dem Untermenü in 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");
}
habe ich versucht, dass es Arbeit den Ball hielt, noch tat das selbe wie vorher –
@Raznish Smith die 'return' in' break' zu ändern funktioniert für m e. –
auch anstelle von 'System.console(). ReadLine()' verwenden Sie 'console.next()' –