我在所有其他应用程序中都有循环,但这似乎无法正常工作。
int i;
BYTE cy; // carry flag
for (cy=1,i=n-1;(i>=0)&&(cy);i--) // process all words from LSW to MSW
{
x[i] = (x[i]-cy)&0xFF; // y[i] = sbc x[i],0
if (x[i]==0xFF) cy=1; // carry
}
即使我将它设置为false,它也会不断重复loop3。之后它应该回到主菜单,但它不会。
我试图隔离问题,没问题。我一直在看代码一小时,我似乎无法找到任何问题。有人知道吗?
孤立的问题:
public void loanBook() {
boolean loop3 = true;
System.out.println("So you wanna loan a book? Excellent choice.");
while (loop3) {
for (int i=0; i<books.size(); i++) {
System.out.println("Search for the book you're looking for: ");
String book = in.nextLine();
if (books.get(i).toString().contains(book) == true) {
System.out.println("Looking for: " + books.get(i).toString() + "?");
System.out.println("press y to loan book or n to try again");
String choice1 = in.next();
if (choice1.equalsIgnoreCase("y")) {
for (int j=0; j<customers.size(); j++) {
if (customers.get(j).getSignedIn() == true) {
customers.get(j).booksLoaned.add(books.get(i));
Timer.delayFunction();
System.out.println(customers.get(j).printBookList());
System.out.println("Returning to main menu");
----> loop3 = false; doesnt work for some reason
}
}
}
}
}
System.out.println("Couldnt find book");
}
}
我的系统输出打印工作正常,所以我知道它正确地通过我的循环。
答案 0 :(得分:1)
你有3个嵌套循环。当loop3
变为false
时,另一个循环(while循环)终止。
但是,您要将loop3
设置为最内圈的false
深度,这意味着while
循环不会检查loop3
的值,直到2内循环完成。
如果你想要从所有循环中断,你需要一些中断语句和/或附加条件。例如:
while (loop3) {
for (int i=0; i<books.size() && loop3; i++) {
System.out.println("Search for the book you're looking for: ");
String book = in.nextLine();
if (books.get(i).toString().contains(book)) {
System.out.println("Looking for: " + books.get(i).toString() + "?");
System.out.println("press y to loan book or n to try again");
String choice1 = in.next();
if (choice1.equalsIgnoreCase("y")) {
for (int j=0; j<customers.size() && loop3; j++) {
if (customers.get(j).getSignedIn()) {
customers.get(j).booksLoaned.add(books.get(i));
Timer.delayFunction();
System.out.println(customers.get(j).printBookList());
System.out.println("Returning to main menu");
loop3 = false;
}
}
}
}
}
System.out.println("Couldnt find book");
}
当loop3
变为false时,这将留下两个内部循环,这将允许外循环立即终止。
答案 1 :(得分:0)
您希望在将其设置为false
后立即退出while循环。你可以用标签
public void loanBook() {
boolean loop3 = true;
System.out.println("So you wanna loan a book? Excellent choice.");
while (loop3) {
outerloop: // added
for (int i=0; i<books.size(); i++) {
System.out.println("Search for the book you're looking for: ");
String book = in.nextLine();
if (books.get(i).toString().contains(book) == true) {
System.out.println("Looking for: " + books.get(i).toString() + "?");
System.out.println("press y to loan book or n to try again");
String choice1 = in.next();
if (choice1.equalsIgnoreCase("y")) {
for (int j=0; j<customers.size(); j++) {
if (customers.get(j).getSignedIn() == true) {
customers.get(j).booksLoaned.add(books.get(i));
Timer.delayFunction();
System.out.println(customers.get(j).printBookList());
System.out.println("Returning to main menu");
loop3 = false;
break outerloop; // added
}
}
}
}
}
System.out.println("Couldnt find book");
}
}