public static void main(String[] args) throws IOException {
ICarnegieInfo companyInfo = ICarnegieInfo.getInstance();
int choice = getChoice();
while (choice != 0) {
if (choice == 1) {
stdOut.println(companyInfo.getName());
} else if (choice == 2) {
stdOut.println(companyInfo.getAddress());
} else if (choice == 3) {
stdOut.println(companyInfo.getTelephone());
} else if (choice == 4) {
stdOut.println(companyInfo.getEmail());
} else if (choice == 5) {
stdOut.println(companyInfo.getUrl());
} else if (choice>5||choice<0) {
stdOut.println("Invalid choice: " + choice);
}
choice = getChoice();
}
}
/**
* get user's input information
* @return the number user is entering
* @throws IOException if error reading from standard input.
*/
private static int getChoice() throws IOException {
int i=0;
while(true)
{
stdOut.println("[0] - Quit");
stdOut.println("[1] - Display name");
stdOut.println("[2] - Display address");
stdOut.println("[3] - Display telephone");
stdOut.println("[4] - Display email");
stdOut.println("[5] - Display URL");
stdOut.printf("choice>");
try
{
i=Integer.parseInt(stdIn.readLine());
break;
}
catch(NumberFormatException e)
{
stdErr.println(e);
}
}
return i;
}
}
循环式菜单
最新推荐文章于 2022-11-09 18:36:47 发布