问题是在while循环中,有一条评论
BufferedReader user = new BufferedReader(new FileReader(
"C:\\Users\\Ionut\\workspace\\tBot\\persoane.txt"));
String line;
while ((line = user.readLine()) != null) {
if (line.toLowerCase().contains(nume.toLowerCase())) {
System.out.println("Ce mai faci " + nume + "?");
ceva = scanIn.nextLine(); //here i want to stop if the condition is true, but it will show the message and go ahead and execute the rest of the code
}
}
最佳答案
基本上有两种常见的解决方案:
1-使用break
:
while ((line = user.readLine()) != null) {
if (line.toLowerCase().contains(nume.toLowerCase())) {
System.out.println("Ce mai faci " + nume + "?");
ceva = scanIn.nextLine();
break; // exists the closest loop
}
}
2-使用
boolean
标志:boolean stop = false;
while (!stop && (line = user.readLine()) != null) {
if (line.toLowerCase().contains(nume.toLowerCase())) {
System.out.println("Ce mai faci " + nume + "?");
ceva = scanIn.nextLine();
stop = true;
}
}