停止重复输入(扫描仪和阵列)

发布于 2025-01-09 00:13:44 字数 1758 浏览 0 评论 0原文

我对编程还很陌生。目前我正在开发一个uni项目,使用java创建一个基本的文本游戏。我遇到的问题是试图找出如何实现不允许用户输入相同名称的业务规则。我已将其设置为扫描仪读取阵列。我正在使用 Java,这是我第一次使用该论坛,因此我非常感谢所提供的任何帮助,并提前感谢大家!:)

对于糟糕的格式表示歉意,我还不知道如何正确发布。

        try {
            
            // Takes in the number of players 
            int noOfPlayers;
            Scanner s = new Scanner(System.in);
            System.out.println("Enter number of players between 2 and 4");
            noOfPlayers = s.nextInt();

            s.nextLine();
            if (noOfPlayers < 2 || noOfPlayers >= 5) { // limits number of players enter no less than 2 and no greater than 4
                System.out.println("Nope wrong number");
                enterInfo();
                s.close();
                return;
            } else {
                
                // array for storing player names

                String[] names = new String[noOfPlayers];
                
                // iterates through the array depending how many players are selected
                // and takes in String input

                for (int counter = 0; counter < noOfPlayers; counter++) {
                    System.out.println("Enter name of player : " + (counter + 1));
                    names[counter] = s.nextLine();
                    
                    
                    // fix this to stop same name sbeing entered 
                    //if(names.equals(names)) {
                    //  System.out.println("Enter a different name");
                    //  counter--;
                //  }
                    
                    

                }

            }
            
            s.close();
        } catch (Exception e) {
            System.out.println("Problem");

        }

    }

I'm fairly new at programming. Currently im working on a uni project to create a basic text game using java. The problem im having is trying to figure out how to implement a business rule that does not allow a user to enter the same name. I have it set up so the scanner reads into the array. I'm using Java and this is my first time using the forum so i greatly appreciate any help given and thank all of you in advance!:)

And apologies for the poor formatting i dont know how to post properly yet.

        try {
            
            // Takes in the number of players 
            int noOfPlayers;
            Scanner s = new Scanner(System.in);
            System.out.println("Enter number of players between 2 and 4");
            noOfPlayers = s.nextInt();

            s.nextLine();
            if (noOfPlayers < 2 || noOfPlayers >= 5) { // limits number of players enter no less than 2 and no greater than 4
                System.out.println("Nope wrong number");
                enterInfo();
                s.close();
                return;
            } else {
                
                // array for storing player names

                String[] names = new String[noOfPlayers];
                
                // iterates through the array depending how many players are selected
                // and takes in String input

                for (int counter = 0; counter < noOfPlayers; counter++) {
                    System.out.println("Enter name of player : " + (counter + 1));
                    names[counter] = s.nextLine();
                    
                    
                    // fix this to stop same name sbeing entered 
                    //if(names.equals(names)) {
                    //  System.out.println("Enter a different name");
                    //  counter--;
                //  }
                    
                    

                }

            }
            
            s.close();
        } catch (Exception e) {
            System.out.println("Problem");

        }

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一杆小烟枪 2025-01-16 00:13:44

使用Set怎么样? Set 必须拥有所有不同的东西。

与数组 [kim,lee,kim,park] 类似,但在集合 [kim,lee,park] 中

输入 kim =>数组[kim] set[kim] lenth 1 == size 1

input lee =>数组 [kim,lee] set[kim,lee] lenth 2 == size 2

input kim => Array [kim,lee,kim] set[kim,lee] lenth 3 != size 2

那么你所要做的就是告诉他们“请起另一个名字”

String name = s.nextLine();
names[counter] = name;

Set<String> set = new HashSet<String>(); 
set.add(name)

if(names.length != set.size())
{
 System.out.println("Enter a different name");
 counter--;
}

我希望你能理解我糟糕的解释。

How about use Set? Set has to have all different things.

Like in Array [kim,lee,kim,park] but in Set [kim,lee,park]

input kim => Array [kim] set[kim] lenth 1 == size 1

input lee => Array [kim,lee] set[kim,lee] lenth 2 == size 2

input kim => Array [kim,lee,kim] set[kim,lee] lenth 3 != size 2

then all you have to do is tell them "make another name plz"

String name = s.nextLine();
names[counter] = name;

Set<String> set = new HashSet<String>(); 
set.add(name)

if(names.length != set.size())
{
 System.out.println("Enter a different name");
 counter--;
}

I hope you understand my poor explanation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文