2015-11-26 40 views
0

所以我試圖顯示一個錯誤消息,當一個球員不存在,但沒有打印,我不知道我做錯了什麼。這是下面的具體代碼。缺少Java關鍵信息無法正常工作?

if (mplayer.containsKey("Carlsen, Magnus")) 
    { 
    System.out.println("Carlsen, Magnus is on the scoreboard."); 
    } 
    if (!mplayer.containsKey("Lyons, David")) 
    { 
    System.out.println("Lyons, David is not on the scoreboard"); 
} 

任何幫助表示讚賞,我的意思是任何幫助! :)

package javamaptest; 


import java.util.*; 
import java.util.HashMap; 
import java.util.Iterator; 
import java.util.Map; 
import java.util.Set; 

public class JavaMapTest 
{ 

public static void main(String[] args) 
{ 

try 
{ 
Map mplayer = new HashMap(); 

mplayer.put("Carlsen, Magnus", "Country:NOR, Rating:(2876), DOB:1990"); 
mplayer.put("Anand, Viswanathan", "Country:IND, Rating:(2804), DOB:1969"); 
mplayer.put("Caruana, Fabiano", "Country:ITA, Rating:(2803), DOB:1992"); 
mplayer.put("Nakamura, Hikaru", "Country:USA, Rating:(2799), DOB:1987"); 
mplayer.put("Topalov, Veselin", "Country:BUL, Rating(2798), DOB:1975"); 
mplayer.put("Grischuk, Alexander", "Country:RUS, Rating:(2780), DOB:1983"); 
mplayer.put("So, Wesley", "Country:USA, Rating:(2778), DOB:1993"); 
mplayer.put("Kramnik, Vladimir", "Country:RUS, Rating:(2777), DOB:1975"); 
mplayer.put("Giri, Anish", "Country:NED, Rating:(2776), DOB:1994"); 
mplayer.put("Aronian, Levon", "Country:ARM, Rating:(2776), DOB:1982"); 
mplayer.put("Ding, Liren", "Country:CHN, Rating:(2757), DOB:1992"); 
mplayer.put("Vachier-Lagrave, Maxime", "Country:FRA, Rating:(2754), DOB:1990"); 
mplayer.put("Karjakin, Sergey", "Country:RUS, Rating:(2753), DOB:1990"); 
mplayer.put("Navara, David", "Country:CZE, Rating:(2751), DOB:1985"); 
mplayer.put("Tomashevsky, Evgeny", "Country:RUS, Rating:(2749), DOB:1987"); 
mplayer.put("Li, Chao b", "Country:CHN, Rating:(2748), DOB:1989"); 
mplayer.put("Woitaszek, Radoslaw", "Country:POL, Rating:(2746), DOB:1987"); 
mplayer.put("Gelfand, Boris", "Country:ISR, Rating:(2744), DOB:1968"); 
mplayer.put("Adam, Michael", "Country:ENG, Rating:(2740), DOB:1971"); 
mplayer.put("Jakovenko, Dmitry", "Country:RUS, Rating:(2738), DOB:1983"); 
// Below I added a pre existing player 
mplayer.put("Jakovenko, Dmitry", "Country:RUS, Rating:(2738), DOB:1983"); 



Iterator iter = mplayer.entrySet().iterator(); 

while (iter.hasNext()) 
    { 
Map.Entry mEntry = (Map.Entry) iter.next(); 
System.out.println(mEntry.getKey() + " : " + mEntry.getValue()); 
} 
// This I find a player with a given key 
mplayer.get("Carlsen, Magnus"); 
    mplayer.get("Anand, Viswanathan"); 

    System.out.println("----------------------------------------------------------------------------"); 
    System.out.println("Carlson, Magnus key information : " + mplayer.get("Carlsen, Magnus")); 
    System.out.println("Anand, Viswanathan key information : " + mplayer.get("Anand, Viswanathan")); 
    System.out.println("----------------------------------------------------------------------------"); 

    if (mplayer.containsKey("Carlsen, Magnus")) 
    { 
    System.out.println("Carlsen, Magnus is on the scoreboard."); 
    } 
    if (!mplayer.containsKey("Lyons, David")) 
    { 
    System.out.println("Lyons, David is not on the scoreboard"); 
    } 

} 

    catch (Exception e) 
    { 
System.out.println(e.toString()); 
    } 

} 
}   
+0

你得到了什麼輸出以及你想得到什麼輸出? – Confiqure

+0

好吧,它不像我想要的那樣工作。基本上我想輸入一個名字並打印一條信息,例如「卡爾森,馬格努斯在記分牌上。」但是如果玩家不在那裏,而不是像if(!mplayer.containsKey(「Lyons,David」))那樣編寫另一個。只想輸入一次該名稱,並在主板上顯示一條消息,但我不確定每個代碼的代碼。希望你能幫助:) – DParker

回答

1
String testName = "Carlsen, Magnus"; 
System.out.println(testName + " is " + (mplayer.containsKey(testName) ? "" : "not ") + "on the scoreboard."); 

這將輸出的名字的狀態正在測試。

+0

非常感謝你的幫助。簡單而有效:) – DParker