2014-10-30 67 views
0

我只是在學習java幾個星期,所以我仍然是一個noob。我想要下一行打印「先生/小姐」。 +名字+姓氏;然而,我不想輸入這個人的性別。我希望有一個男性名字列表(超過1000個名字的長列表),以及檢測姓氏輸入是否是這些男性名字之一的程序。然後該程序可以將正確的標題(先生/女士)如何從名稱列表中調用?

如何做到這一點而不進行正常ArrayList和獨立打字每名(which'll採取agessss)。

在此先感謝!

public static void main (String [] args){ 

    Scanner scanner = new Scanner(System.in); 
    System.out.println("Hello, will you be checking out today? (Y/N)"); 
    String CheckOutYesOrNo = scanner.nextLine(); 

    if (CheckOutYesOrNo.equalsIgnoreCase("y")) { x();} 
    else if (CheckOutYesOrNo.equalsIgnoreCase("n")) {System.out.println("Okay then. Enjoy the rest of your stay at the Rizty Hotel!");} 
    } 

    public static void x(){ 
     System.out.println("Sure, would you mind telling me your last name?"); 
     Scanner scanner = new Scanner(System.in); //how can I avoid making a new scanner? 
     String lastname = scanner.nextLine(); 

     System.out.println("And your first name?"); 
     String firstname= scanner.nextLine(); 





    } 
} 
+0

那可能是什麼名字呢? (Bailey,Sam等) – vcapra1 2014-10-30 19:36:22

+0

你有.txt文件中的名字嗎? – 2014-10-30 19:38:15

+0

有一個原因,基本上每個系統都從用戶那裏收集標題 - 不僅僅是性別。 – 2014-10-30 19:38:20

回答

0

聽起來像一個典型的映射。您可以將所有姓名讀入Map,其中關鍵是姓名,性別是價值。要解決性別問題,只需執行如genderMap.get(name)這樣的操作。

相關問題