在java中,从子菜单返回主菜单
因此,我当前的代码有一个问题,当我点击exit(4)时,它不会从子菜单返回主菜单,它只是在主菜单while循环中重复if语句,从而启动子菜单。我该如何解决这个问题在java中,从子菜单返回主菜单,java,menu,return,Java,Menu,Return,因此,我当前的代码有一个问题,当我点击exit(4)时,它不会从子菜单返回主菜单,它只是在主菜单while循环中重复if语句,从而启动子菜单。我该如何解决这个问题 import java.util.Scanner; import java.util.*; import java.util.ArrayList; public class StarberksInterface { public static void main(String args[]) { Scanner con
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");
}
import java.util.Scanner;
导入java.util.*;
导入java.util.ArrayList;
公共类StarberksInterface
{
公共静态void main(字符串参数[])
{
扫描仪控制台=新扫描仪(System.in);
store=新商店();
字符串str,sName1,sName2,name;
字符c;
int n=0;
sName1=“卡拉汉”;
sName2=“Lambton”;
//这是将首先显示的主菜单。
System.out.println(“管理系统主菜单”);
System.out.println(“======================================================================”);
System.out.println(“1.选择存储”);
System.out.println(“2.显示存储”);
System.out.println(“3.通过文件加载存储”);
System.out.println(“4.将存储保存到文件”);
System.out.println(“5.退出程序”);
System.out.println(“======================================================================”);
while(n!=5)//按下4时退出程序
{
System.out.print(“\n请输入选项1-4以继续…:”;
n=Integer.parseInt(System.console().readLine());
//读取用户输入并将其带到所选代码。
如果(n>5 | | n5 | | n您需要将主菜单移动到此循环中:
while(n!=5)/
{
...
}
更改中的return语句
if (sName1.equals(name) || sName2.equals(name)) {
StarberksInterface.subMenu();
return;
}
到
要再次打印菜单,您需要将主菜单移动到循环中:
while (n != 5)// Exits the program when 4 is pressed{
.....
}
试着替换
n = Integer.parseInt(System.console().readLine());
借
在这两个地方我都试过了,但都没用,还是做了和我一样的事before@Raznish史密斯将返回
更改为中断
对我来说很有效。也可以使用console.next()代替System.console().readLine()
使用console.next()
n = Integer.parseInt(System.console().readLine());
n = console.nextInt();