gpt4 book ai didi

java - 调用方法并返回菜单

转载 作者:行者123 更新时间:2023-12-02 00:28:16 25 4
gpt4 key购买 nike

我已经设置了一个打印到控制台的“菜单”。接受用户输入,调用相应方法,然后返回菜单以获取进一步说明。我应该如何构建我的代码,以便它在完成正在做的事情后输出“菜单”?

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");
}
}

}

最佳答案

你一定要看看java的switch语句:http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

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

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

关于java - 调用方法并返回菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9624795/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com