我可能错过了一些非常简单的东西,但我是新人,寻求帮助更容易。这是家庭作业,因此您可以根据需要提供线索,但请理解我对 java 的了解很糟糕。
下面是地址簿的一些代码。我希望用户输入 1 查看条目,输入 2 将另一个条目放入书中,或者输入 3 退出。 我让程序运行,但它没有循环询问用户下一步该做什么。然后我编写了一个 switch 语句,当用户选择 1 时,程序不会运行与情况 1 相关的代码:并且与情况 2 相同。该程序确实验证了我的条目(我在单独的类中编写了一个 validator )
我在编码时错过了什么吗?
再说一遍,我是新人,所以不要打败我。
import java.util.Scanner;
public class AddressBookEntryApp
{
public static void main(String[] args)
{
//create new scanner
Scanner ip = new Scanner(System.in);
//welcome user to the address book application
System.out.println("Welcome to the Address Book Application");
System.out.println();
int choice = 0;
boolean quit = false;
do
{
//have the user enter a menu number
System.out.println("1 - List entires");
System.out.println("2 - Add entry");
System.out.println("3 - Exit");
System.out.println();
int menuNumber = Validator.getInt(ip, "Enter menu number: ", 1, 3);
System.out.println();
switch (choice)
{
case 1:
AddressBookIO GetEntryObject = new AddressBookIO();
GetEntryObject.getEntriesString();
System.out.println(AddressBookIO.getEntriesString());
break;
case 2:
String name = Validator.getEntry(ip, "Enter name: ");
String email = Validator.getEntry(ip, "Enter email address");
String phone = Validator.getEntry(ip, "Enter phone number: ");
AddressBookEntry newEntry = new AddressBookEntry(name, email, phone);
AddressBookIO.saveEntry(newEntry);
break;
}
}while (!quit);
}
}
最佳答案
选择
设置为0
。您是否想打开menuNumber
?
关于java - 代码不在 switch 语句内执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9601784/