我可能錯過了一些非常簡單的事情,但我是新的,它只是更容易尋求幫助。這是作業,所以如果你想要的話你可以提供線索,但是請理解我對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