Java 调用一个方法并返回菜单

Java 调用一个方法并返回菜单,java,Java,我已经设置了一个“菜单”,可以打印到控制台。接受用户输入,根据方法调用,然后返回菜单以获得进一步指示。我应该如何构造代码,使其在完成任何操作后输出“菜单” public static void main(String[] args)throws Exception { // TODO Auto-generated method stub Scanner keyboard = new Scanner(System.in); EntryNode n = new EntryNo

我已经设置了一个“菜单”,可以打印到控制台。接受用户输入,根据方法调用,然后返回菜单以获得进一步指示。我应该如何构造代码,使其在完成任何操作后输出“菜单”

public static void main(String[] args)throws Exception {
    // TODO Auto-generated method stub
    Scanner keyboard = new Scanner(System.in);
    EntryNode n = new EntryNode();
    AddressList addressBook = new AddressList();

    String menu = " ";

    System.out.println("******************************************************************");
    System.out.println("Welcome to the Jackie 2000 Address Book");
    System.out.println("What do you want to do? ");
    System.out.println("[p] View All Entries in Address Book  [a] Add New Entry");
    System.out.println("[d] Remove An Entry                   [s] Search for Entry");
    System.out.println("[i] Import Address Book               [x] Export Address Book");
    System.out.println("[z] Exit");

    System.out.println();
    System.out.println("Please enter your choice: ");
    menu = keyboard.next().toLowerCase();

    if (menu.equals("p")) {
        try {
            addressBook.printList();
        }
        catch (Exception e){

        }
    }
    else if (menu.equals("a")) {
        System.out.println("Enter in the first name ");
        String firstName = keyboard.next().toUpperCase();
        System.out.println("Enter in the last name ");
        String lastName = keyboard.next().toUpperCase();
        System.out.println("Enter in the phone number");
        String phoneNum = keyboard.next().toUpperCase();
        System.out.println("Enter in the email");
        String email = keyboard.next().toUpperCase();
        addressBook.addEntry(firstName,lastName,phoneNum,email);
    }
    else if (menu.equals("d")) {
        EntryNode temp = head;
        for (int i = 0; i <addressBook.length(); i++) {
            System.out.println(i + " Name: " + temp.getFirstName() + " " + temp.getLastName() + " " 
        + temp.getPhoneNum() + " " + temp.getEmail());
            temp = temp.getNext();
        }
        System.out.println(" ");
        System.out.println("Please enter the index of the entry you wish to delete ");
        int index = keyboard.nextInt();
        addressBook.removeEntry(index);
    }

    else if (menu.equals("s")) {
        System.out.println("Do you want to search by email or name? ");
        String decision = keyboard.next();
        if (decision.equals("email")) {
            System.out.println("What email address are you looking for? ");
            String email = keyboard.next();
            addressBook.searchEmail(email);
        }
        else if (decision.equals("name")) {
            System.out.println("What name are you looking for?");
            String name = keyboard.next();
            addressBook.searchEntry(name);
        }
        else System.out.println("Invalid entry. Type in 'email' or 'name'");
    }

    else if (menu.equals("i")) {
        addressBook.importBook();
    }

    else if (menu.equals("x")) {
        addressBook.exportBook();
    }

    else if (menu.equals("e")) {
        System.exit(0);
    }
    else {
        System.out.println("Invalid Entry");
    }
}
publicstaticvoidmain(字符串[]args)引发异常{
//TODO自动生成的方法存根
扫描仪键盘=新扫描仪(System.in);
EntryNode n=新的EntryNode();
AddressList addressBook=新地址列表();
字符串菜单=”;
System.out.println(“**********************************************************************************************************************”);
System.out.println(“欢迎使用Jackie 2000通讯簿”);
System.out.println(“您想做什么?”);
System.out.println(“[p]查看通讯簿中的所有条目[a]添加新条目”);
System.out.println(“[d]删除条目[s]搜索条目”);
System.out.println(“[i]导入地址簿[x]导出地址簿”);
System.out.println(“[z]退出”);
System.out.println();
System.out.println(“请输入您的选择:”);
菜单=键盘.next().toLowerCase();
if(菜单等于(“p”)){
试一试{
addressBook.printList();
}
捕获(例外e){
}
}
else if(menu.equals(“a”)){
System.out.println(“输入名字”);
String firstName=keyboard.next().toUpperCase();
System.out.println(“输入姓氏”);
字符串lastName=keyboard.next().toUpperCase();
System.out.println(“输入电话号码”);
字符串phoneNum=keyboard.next().toUpperCase();
System.out.println(“输入电子邮件”);
字符串email=keyboard.next().toUpperCase();
addEntry(firstName、lastName、phoneNum、email);
}
else if(menu.equals(“d”)){
入口节点温度=头部温度;

对于(int i=0;i将一个菜单printlns放入一个单独的静态方法。在每次addressBook方法调用之后,在关闭else if之前调用它,退出案例除外。

将一个菜单printlns放入一个单独的静态方法。在每次addressBook方法调用之后,在关闭else if之前调用它,退出案例除外。

如果需要same
菜单
要在用户输入一个选项并且程序执行了他必须执行的操作后再次显示,只需将整个过程置于
while
do…while
循环中,并且仅在用户选择退出选项时退出它。

如果您希望在用户输入一个选项后再次显示相同的
菜单
choice和程序执行了他必须执行的操作,只需将整个过程放入
while
do…while
循环中,并且只有在用户选择退出选项时才退出它。

您一定要看看java的switch语句:

您可以将整个switch case语句放在while循环中,并使用一个布尔值表示何时应该退出。例如:

while(!exit){
    switch(input){
         case "a": do something;break;
         case "d":...
         ...
         case "e": exit = true;
    }
}

您肯定应该看看java的switch语句:

您可以将整个switch case语句放在while循环中,并使用一个布尔值表示何时应该退出。例如:

while(!exit){
    switch(input){
         case "a": do something;break;
         case "d":...
         ...
         case "e": exit = true;
    }
}