我已经设置了一个打印到控制台的“菜单”。进行用户输入,按方法调用,然后返回菜单进行进一步指令。我应该如何构建我的代码,以便它在完成任何正在进行的操作后输出“菜单”?
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");
}
}
}
答案 0 :(得分:2)
你一定要看一下java的switch语句:http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html
你可以将一个整个switch-case语句放在一个while循环中,并在它应该退出时使用一个布尔值。例如:
while(!exit){
switch(input){
case "a": do something;break;
case "d":...
...
case "e": exit = true;
}
}
答案 1 :(得分:1)
如果您希望在用户输入选项并且程序执行了他必须执行的操作后再次显示相同的menu
,只需将整个过程放在while
或{{1}中} loop并仅在用户选择退出选项时退出。
答案 2 :(得分:0)
将菜单printlns放入单独的静态方法中。在每个addressBook方法调用之后调用它,就在关闭else之前,除了退出情况之外。