gpt4 book ai didi

java - 切换菜单Java

转载 作者:行者123 更新时间:2023-11-29 04:54:39 26 4
gpt4 key购买 nike

我有一个切换菜单,我希望它循环整个菜单,包括方向,但它只是不断循环我选择的操作。我该如何改变它?我从 do/while 切换到 while 循环。

 int count = 0;

String first;
String last;
String num;
Contact person;

System.out.println("What would you like to do?");
System.out.println("Type c to create");
System.out.println("Tpe e to edit");
System.out.println("Tpe d to delete");
System.out.println("Type q to quit");

Scanner input = new Scanner(System.in);
char choice = input.next().charAt(0);

AddressBook addressBook = new AddressBook();
while ('q' != choice) {
switch (choice) {

case 'c':
System.out.println("Enter first name, last name and phone number");

addressBook.addContact();

count++;

System.out.println("Total number of contact: " + count);

break;

case 'e':
System.out.println("Enter name to be edited");

first = input.next();
last = input.next();
num = null;

person = new Contact(first, last, num);
addressBook.edit(person);

break;
case 'd':
System.out.println("Enter name to be deleted");

first = input.next();
last = input.next();
num = null;

person = new Contact(first, last, num);

addressBook.removeContact(person);
break;
default:
System.out.println("Operation does not exist");
}

}
}

最佳答案

char choice 初始化为默认字符:

char choice = 'a';

然后移动所有这些:

System.out.println("What would you like to do?");
System.out.println("Type c to create");
System.out.println("Tpe e to edit");
System.out.println("Tpe d to delete");
System.out.println("Type q to quit");

choice = input.next().charAt(0);

在你的 while 循环中:

while ('q' != choice) {
//show menu options
//allow user to select a menu option

//use switch to operate based on user decision

//once the operation is complete, as long as the user didn't select q,
//the menu options show once more and allow another selection
}

关于java - 切换菜单Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34229578/

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