相当刚刚开始使用Java,并且刚刚开始制作地图,尝试了几天但取得了有限的成功,以便执行以下操作(代码中的注释)欢迎所有建议。理想情况下,我想从一个未填充的地图开始,用纯粹的用户输入playerName填充它,并在输入每个名称时将整数值递增1。希望这遵循所有准则!
/**
* Constructor for objects of class player Creates player
* objects and tests for unique player name
*/
public player() {
Map<String, Integer> playerMap = new HashMap<>();
System.out.println("Players in game: " + playerMap.isEmpty());
playerMap.put("Fred", 1);
playerMap.put("Bob", 2);
playerMap.put("Alice", 3);
playerMap.put("Charlotte", 4);
playerMap.put("Catherine", 5);
System.out.println("Players in game: " + playerMap.size());
/**
* user input via dialogue to get another playerName String
* playerName set to Catherine for testing
*/
String playerName = "Catherine";
boolean existing = playerMap.containsValue(playerName);
System.out.println(existing);
if (existing = true) {
int playerNumber = 5;
/**
* user input via dialogue to get another playerName I need
* to set playerNumber = highest existing key in map then
* increment by one then add values to map loop until all
* players have been entered, I am considering while
* (playerName != "last"); playerNumber set to 5 for
* testing
*/
playerNumber = playerNumber++;
playerMap.put(playerName, playerNumber);
}
}
答案 0 :(得分:0)
方法containsValue
检查地图中是否已存在值。
“凯瑟琳”不是一个价值观。这是关键。
改为使用containsKey
。
或者你可能已经反转了键和值。因此,创建一个Map<Integer, String>
并在其上放置值。
答案 1 :(得分:0)
您可以iterate
而不是containsValue
,因为containsValue会在内部遍历整个地图。
private boolean exists(String playerName){
boolean isExists=false;
for(Map.Entry<String, Integer> playerEntry:playerMap.entry()){
if(playerEntry.getValue().equals(playerName){
isExists=true;
break;
}
}
return isExists;
}
如果map
不包含它将遍历整个地图的值,则为50-50。如果它包含值,它将断开循环并返回true
。
然后,您可以使用以下逻辑将put
导入playerMap
private void addToMap(String playerName){
if(playerName!=null && exists(playerName)){
playerMap.put(playerName, ++playerNumber);
}
}
答案 2 :(得分:0)
您的if语句(existing = true)错误为单个&#34; =&#34;是分配而不是比较。如果要比较两个布尔值的相等性,则使用double等于&#34; ==&#34;比较。事实上,你甚至不需要检查现有的&#34;变量是真的,因为它已经是一个布尔值,只用if语句包围变量,如果是真,它将进入if块,否则它将赢得。目前,您的代码将始终输入,如果代码阻止,因为您正在分配&#34;现有&#34;变量为true。
同样如前一张海报所提到的,你使用名称作为键而不是值,所以你应该使用containsKey()方法而不是containsValue()。
如果您的地图包含密钥&#34; Catherine&#34;
,代码下方只应输入if块 String playerName = "Catherine";
boolean existing = playerMap.containsKey(playerName);
if (existing) {
int playerNumber = 5;
/**
* user input via dialogue to get another playerName I need
* to set playerNumber = highest existing key in map then
* increment by one then add values to map loop until all
* players have been entered, I am considering while
* (playerName != "last"); playerNumber set to 5 for
* testing
*/
playerNumber = playerNumber++;
playerMap.put(playerName, playerNumber);
}
答案 3 :(得分:0)
此代码似乎已经完成了这一操作,并列出了所有已输入的玩家名称,感谢所有帮助,只想发布决赛,欢迎任何其他评论!非常享受java ....
public player()
{
Map<Integer, String> playerMap = new HashMap<>();
int playerNumber = 0;
do
{
String playerName = 'user input request dialogue box' ("Please input a unique player name (5 Maximum): " + ((playerMap.size() - 10) * -1) + " Player slots left.");
boolean existing = playerMap.containsValue(playerName);
System.out.println(existing);
if (existing == false)
{
playerNumber = ++playerNumber;
playerMap.put(playerNumber, playerName);
System.out.println("Number of players in game: " + playerMap.size());
}
}
while (playerNumber < 5);
System.out.println("Players in game are:");
for (int a : playerMap.keySet())
{
System.out.println("Player " + a + " is \"" + playerMap.get(a) + "\"" );
}
}