这个循环第一次迭代效果很好,但是在我按下 char 'y' 以重复之后,下次它显示时,它不会让我输入另一个名字。我不知道是什么原因造成的,但是当您需要清除输入缓冲区时,它让我想起了 C。
任何帮助都将不胜感激。
byte counter = 1;
boolean myCondition = false;
List<String> myList = new ArrayList();
BufferedReader objReader = new BufferedReader(new InputStreamReader(System.in));
do{
System.out.println("Enter the #" +counter +" person's name:");
// low quality validation
String dude = objReader.readLine();
while (myList.contains(dude)){
System.out.println("Man, this dude:" +dude +" is already loaded into the List! Pick another name:\n");
dude = objReader.readLine();
}
myList.add(dude);
counter++;
System.out.println("Would you like to add another name to the list? yes/no");
char myChar = (char) System.in.read();
if (myChar == 'y' || myChar == 'Y')
myCondition = true;
else
myCondition = false;
} while (myCondition);