我创建了一个保存 [24] 数据的数组,并在每个索引中分配了一些信息。我的问题是,当我想从键盘上使用扫描仪调用索引时,假设我从用户处调用了索引[12],下次调用它时我想说,你已经选择了该号码,请选择一个不同的号码,这样等等。基本上,我不应该两次调用同一个索引,最好使用什么。
非常需要您的帮助。
最佳答案
使用java.util.Set
来存储选定的索引,例如java.util.HashSet
。
它应该看起来像:
Set<Integer> selected = new HashSet<>();
int userInput = ...; // get input from user
while (selected.contains(userInput)) {
// print u already selected that number, choose a different number so on so forth
userInput = ...; // get input from user
}
selected.add(userInput);
// do something with the index
关于java - 如何阻止用户再次选择相同的号码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53238001/