将用户输入添加到映射并将整数键递增 1


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);
    }
}

该方法containsValue检查映射中是否已存在值。

"凯瑟琳"不是一个价值。这是一把钥匙。

请改用containsKey

或者您可能已经反转了键和值。因此,创建一个Map<Integer, String>并在其上放置值。

你可以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;
 }

它是 50-50,以防map不包含它将遍历整个地图的值。如果它包含该值,它将断开循环并返回true .

然后,您可以使用以下逻辑来put playerMap

private void addToMap(String playerName){
 if(playerName!=null && exists(playerName)){
   playerMap.put(playerName, ++playerNumber);
 }
}

你的 if 语句(现有 = true)是错误的,因为单个"="是赋值而不是比较。如果要比较两个相等的布尔值,请使用双等号"=="进行比较。实际上,您甚至不需要检查"现有"变量是否为真,因为它已经是一个布尔值,只需用 if 语句将变量括起来,如果它是真的,它将进入 if 块,否则它不会。目前,您的代码将始终输入 if 代码块,因为您将"现有"变量分配给 true。

此外,正如之前的海报所提到的,您使用名称作为键而不是值,因此您应该使用 containsKey() 方法而不是 containsValue()。

下面的代码应该只在你的地图包含键"凯瑟琳"时才输入 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);
}

这段代码似乎已经完成了这个技巧,并列出了所有已输入的球员姓名,感谢所有的帮助,只是想发布决赛,欢迎任何其他评论!非常喜欢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) + """ );

}
}

相关内容

  • 没有找到相关文章

最新更新