SSD 3 Practical Quiz 1 答案

import java.io.*;
import java.util.*;

/**
 * @version 1.0.0
 * @author vivizhyy
 */
public class ICarnegieInfoApplication {

    private static BufferedReader stdIn = new BufferedReader(
            new InputStreamReader(System.in));
    private static PrintWriter stdOut = new PrintWriter(System.out, true);
    private static PrintWriter stdErr = new PrintWriter(System.err, true);
    private int choice;

    /* DOCUMENT THIS PUBLIC METHOD */
    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());
            }

            choice = getChoice();
        }
    }

    private static int getChoice() throws IOException {
        int input = 0;
        /* PLACE YOUR CODE HERE */

        try {
            stdErr
                    .print("[0] Quit/n[1] Display name/n[2] Display address"
                            + "/n[3] Display telephone/n[4] Display email/n[5] Display URL"
                            + "/nchoice>");
            stdErr.flush();
            input = Integer.parseInt(stdIn.readLine());
            if (input < 0 || input > 5) {
                System.out.println("Invalid choice: " + input);
            }
        } catch (NumberFormatException nfe) {
             nfe.printStackTrace();//写一大段
            // System.out.println(nfe.getMessage()); the output result
            // was just "For input string "aa""
            // System.out.println(nfe.getCause()); output "null"
            // System.out.println(nfe.getLocalizedMessage()); the same as
            // .getMessage
            //stdErr.println(nfe.getMessage());// the same as .getMessage
            input=getChoice();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
        return input; /* CHANGE THIS STATEMENT AS NEEDED */
    }

}
原文地址:https://www.cnblogs.com/vivizhyy/p/3394924.html