2013-05-08 170 views
0

我正在嘗試創建類似於(非常)基本的在線購物應用程序的(非常)簡單的菜單/子菜單導航系統。
我得到的問題是,一旦我進入了子菜單(通過在頂層菜單中輸入2),我無法離開子菜單;即使輸入3或4,我不知道爲什麼會發生這種情況,任何幫助,將不勝感激。無法退出(嵌套)while循環 - Java

 while (subChoice != 3 || subChoice != 4) { 
      subMenu(); 
      subChoice = getChoice(1, 4); 
      if (subChoice == 1) { 
       // Add items 
       System.out.println("add"); 
      } else if (subChoice == 2) { 
       // Remove items 
       System.out.println("delete"); 
      } else if (subChoice == 3) { 
       // Check out 
       System.out.println("check out"); 
      } else if (subChoice == 4) { 
       // Discard cart 
       System.out.println("discard"); 
      } 
     } 
+0

打開調試器並自行回答。不要垃圾SO。 – Val 2013-05-08 12:17:19

回答

2

while (subChoice != 3 || subChoice != 4) {是測試如果subChoiceNOT 3或是NOT 4.它不能同時既,因此該循環永遠不會結束。要修復它,請使用以下任一選項:

while (subChoice != 3 && subChoice != 4) { 
    ... 
} 
while (!(subChoice == 3 || subChoice == 4)) { 
    ... 
}