所以我试图在播放器不存在时显示错误消息,但没有打印出来,我不确定我做错了什么。这是下面的具体代码。
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 :(得分:1)
String testName = "Carlsen, Magnus";
System.out.println(testName + " is " + (mplayer.containsKey(testName) ? "" : "not ") + "on the scoreboard.");
这将输出您正在测试的名称的状态。