【问题标题】:Java: implement a loop with duplicate values in ArrayJava:在数组中实现具有重复值的循环
【发布时间】:2015-01-22 22:01:52
【问题描述】:

我的任务是将名称输入到数组中。如果该名称已经输入,程序必须对此发出警告并提供重新输入相同号码的玩家。

这是我的代码:

public void enterNames()    {

     for (int i=0; i<nameOfPlayers.length; i++) 
     {
           do 
           {    
               // isDuplicate is a Boolean initialized to false     
               System.out.println("CHECK": + isDuplicate);

               System.out.println("Enter player " + (i+1) + ":");

               nameOfPlayers[i] = in.next();

               for (int k=0; k<nameOfPlayers.length; k++)   
               {
                   if (k!=i && nameOfPlayers[i].equals(nameOfPlayers[k]))   
                   {
                       isDuplicate = true;                      
                       break;
                   }            
               }
           } while (isDuplicate = false);
      } 
 }

有趣的是,即使我输入了一个重复的值,它也会被捕获并将true分配给isDuplicate,但是当它返回到while循环的开头时,该值再次为false(“CHECK:false”)。

看起来很容易,但我被抓住了......

另外,我不想使用 HashSet,只想使用 Array。

非常感谢!

编辑:

感谢其他人,我将代码重写为以下内容:

public void enterNames()    {

    List<String> nameOfPlayersList = new ArrayList<String>();
    int i = 0;

    for (i=0; i<numberOfPlayers;)   
    {
        while(true) 
        {
            System.out.println("Enter player " + (i+1) + ":");
            String input = in.next();

            if(!nameOfPlayersList.contains(input))  
            {
                nameOfPlayersList.add(input);
                i++;
                break;
            }
            System.out.println("Player " + input + " already exists, please retry");
        }
    }
}

【问题讨论】:

    标签: java arrays loops duplicates


    【解决方案1】:

    你遇到的问题是因为

    } while (isDuplicate = false);
    

    应该是(注意双重==)

    } while (isDuplicate == false);
    

    除此之外,您的代码效率很低。如果这确实是您想要的,那么使用两个数组可能会做得更好,否则最好使用链表。

    【讨论】:

    • 谢谢。我完全忘记了 while 也需要 == 。 :-)
    【解决方案2】:

    你的while不正确,这个

    while (isDuplicate = false);
    

    false 分配给isDuplicate,其副作用也是评估false。你想要类似的东西

    while (isDuplicate == false);
    

    或更短的

    while (!isDuplicate);
    

    【讨论】:

    • 谢谢。我完全忘记了 while 也需要 == 。 :-)
    【解决方案3】:

    答案改头换面,用List添加越来越多的没有预先定义大小的元素。

    while (isDuplicate == false); 更改为while (!isDuplicate);

     public static void main(String[] args) {
    
            Scanner scanner = new Scanner(System.in);
            List<String> nameOfPlayers = new ArrayList<String>();
            boolean isDuplicate = false;
            int i = 0;
    
            do {
                System.out.println("Enter player " + (i + 1) + ": or Q for Quit");
                String input = scanner.next();
    
                if (!input.equalsIgnoreCase("Q")) {
                    if (nameOfPlayers.contains(input)) {
                        isDuplicate = true;
                    } else {
                        nameOfPlayers.add(input);
                        isDuplicate = false;
                    }
                    System.out.println("CHECK : " + isDuplicate);
                } else {
                    break;
                }
    
                i++;
            } while (!isDuplicate);
        }
    
    
    Enter player 1: or Q for Quit
    ankur
    CHECK : false
    Enter player 2: or Q for Quit
    singhal
    CHECK : false
    Enter player 3: or Q for Quit
    ankur
    CHECK : true
    

    【讨论】:

    • 非常感谢!我以不同的方式重新编写了我的代码,但您的解决方案提示了如何去做。
    猜你喜欢
    • 2012-01-08
    • 2014-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-14
    • 1970-01-01
    • 2016-08-08
    • 1970-01-01
    相关资源
    最近更新 更多