【发布时间】:2019-01-16 03:31:08
【问题描述】:
我对编程很陌生,我希望能够防止用户重复输入字符串。但到目前为止,下面的代码不起作用(或被程序完全忽略)。提前致谢!!
boolean current = false;
for (int i = 0; i < dogs.size(); i++) {
System.out.println("Dog name: ");
String dogName = scan.nextLine().toLowerCase();
if (dogName.equals(dogs.get(i).getName())) {
auction.add(new auction(dogName));
System.out.printf(dogName + " has been put up for auction in auction #%d", i);
System.out.println();
current = true;
}//code below does not work
if (auction.contains(dogName)) {
System.out.println("Error: this dog is already up for auction.");
}
}
if (current == false) {
System.out.println("Error: no such dog in the register");
}
【问题讨论】:
-
您需要搜索所有
dogs,而不仅仅是dogs(i)以验证是否有重复项。 -
什么是“拍卖”类?