停止重复输入(扫描仪和阵列)
我对编程还很陌生。目前我正在开发一个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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用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
那么你所要做的就是告诉他们“请起另一个名字”
我希望你能理解我糟糕的解释。
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"
I hope you understand my poor explanation.