gpt4 book ai didi

java - 我应该如何让我的开关回到主开关?

转载 作者:行者123 更新时间:2023-12-04 06:34:03 27 4
gpt4 key购买 nike

美好的一天......我正在创建一个地址簿程序......我在我的主菜单中使用了一个开关......并计划为我的编辑菜单创建另一个开关......我的问题是......我不知道如何回到我的主开关……这是我的主程序:

import javax.swing.JOptionPane;
import javax.swing.JTextArea;

public class AddressBook {

private AddressBookEntry entry[];
private int counter;

public static void main(String[] args) {
AddressBook a = new AddressBook();
a.entry = new AddressBookEntry[100];
int option = 0;
while (option != 6) {
String content = "Choose an Option\n\n"
+ "[1] Add an Entry\n"
+ "[2] Delete an Entry\n"
+ "[3] Update an Entry\n"
+ "[4] View all Entries\n"
+ "[5] View Specific Entry\n"
+ "[6] Exit";
option = Integer.parseInt(JOptionPane.showInputDialog(content));
switch (option) {
case 1:
a.addEntry();
break;
case 2:

break;
case 3:
a.editMenu();
break;
case 4:
a.viewAll();
break;
case 5:
break;
case 6:
System.exit(1);
break;
default:
JOptionPane.showMessageDialog(null, "Invalid Choice!");
}
}
}

public void addEntry() {
entry[counter] = new AddressBookEntry();
entry[counter].setName(JOptionPane.showInputDialog("Enter name: "));
entry[counter].setAdd(JOptionPane.showInputDialog("Enter add: "));
entry[counter].setPhoneNo(JOptionPane.showInputDialog("Enter Phone No.: "));
entry[counter].setEmail(JOptionPane.showInputDialog("Enter E-mail: "));
counter++;
}

public void viewAll() {
int i = 0;
for (; i < counter; i++) {
JOptionPane.showMessageDialog(null, new JTextArea(entry[i].getInfo()));
}
}

public void editMenu() {
int option = 0;
while (option != 6) {
String content = "Choose an Option\n\n"
+ "[1] Edit Name\n"
+ "[2] Edit Address\n"
+ "[3] Edit Phone No.\n"
+ "[4] Edit E-mail address\n"
+ "[5] Go Back to Main Menu\n";
option = Integer.parseInt(JOptionPane.showInputDialog(content));
switch (option) {
case 1:
editEntry();
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
default:
JOptionPane.showMessageDialog(null, "Invalid Choice!");
}
}
}

public void editEntry() {
String EName;
EName = JOptionPane.showInputDialog("Enter name to edit: ");
for (int i = 0; i < counter; i++) {
if (entry[i].getName().equals(EName)) {
entry[i].setName(JOptionPane.showInputDialog("Enter new name: "));
}
}
}
}

请帮助...提前致谢:)

最佳答案

要返回给调用者,您可以使用 return;而不是 break;
在编辑菜单中

            case 5:
return;

但是,我怀疑你的问题是
while (option != 6) {

应该
while (option != 5) {

您还可以使用标签来跳出 while 循环,这在这里会做同样的事情。

关于java - 我应该如何让我的开关回到主开关?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5041828/

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