我可能错过了一些非常简单的事情,但我是新的,它只是更容易寻求帮助。这是作业,所以如果你想要的话你可以提供线索,但是请理解我对java很恐怖。在switch语句中没有执行的代码
以下是地址簿的一些代码。我希望用户输入1来查看条目2以在书中放入另一个条目或者3放弃。 我得到了程序工作,但它没有循环询问用户下一步该做什么。然后,我编写了一个switch语句,当用户选择1时,程序不会运行与case 1相关的代码:并且与case 2相同。程序确实验证了我的输入(我在一个单独的类中编写了验证器)
编码时我错过了什么吗?
再次我是新来的,所以不要打我。
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);
}
}
这似乎工作!那么我没有必要声明一个选择呢?我一直在为其他程序做这件事,所以这其中的一些情况已经没有了。 – 2012-03-07 12:53:00
或者您可以将选定的菜单编号直接分配给变数“选择”。这也会起作用。检查我的答案。 – 2012-03-07 12:57:14