2013-11-01 134 views
2

我正在學習Java編程,並在此代碼中遇到問題。我的問題是我不能停止while循環(while (done ==1){ .. }Java while循環不想停止

如果我做= 2,程序恢復二...

int stopme3 = 1; 
while (stopme3 == 1) {   
    /* Appel de la méthode Afficher les propriétaires */ 
    AfficherProprio(); 
    int choix_proprio = ChoisirProprio(); 

    /* Appel de la méthode Afficher les Comptes du Propriétaire */ 
    AfficherComptesProprietaire(choix_proprio); 

    /* Choix du compte à modifier */ 
    System.out.println("N° de compte:"); 
    int choixCompte = lectureClavier.nextInt(); 
    /* Test si comptes existants du proprio */ 
    if (choixCompte == tab_compte[choix_proprio]._num_compte) { 
     int done = 1; 
     while (done == 1) { 
      /* Création d'une ligne comptable */ 
      tab_compte[choix_proprio].CreerLigneC(); 
      System.out.println("Ajouter une ligne comptable supplémentaire ?"); 
      System.out.println("1 - Oui"); 
      System.out.println("2 - Non"); 
      done = lectureClavier.nextInt(); 
     } 
    } else { 
     System.out.println("Compte sélectionné inexistant."); 
    } 
} 

幫助非常感謝,非常感謝。

+2

啊,漂亮的法國代碼,非常可讀的互聯網上的每個人。他在'done = lectureClavier.nextInt();''lectureClavier'似乎意味着鍵盤改變'done'。 – Philipp

+1

來自'lectureClavier.nextInt()'的數據是什麼? – Epsilon

+0

問題不在'stopme3'中的'done'中,它保持爲int,外部循環繼續進行。 – Philipp

回答

3
int stopme3 = 1; 
      while (stopme3 == 1) { // This loop will keep running till condition is true 


int done = 1; 
    while (done == 1) {// This loop will keep running till condition is true 

因此,這將無限運行。因此,你可能想這樣做

int stopme3 = 1; 
    while (stopme3 == 1) { 

    if(some condition is met){ 
      stopme3 =2; 
     } 

} 
+0

他在談論內循環(while(done == 1){..})'不是有限的。你的建議是關於外部循環。 –

+0

非常感謝你,它現在可以工作。 :) 我已添加: if(done!= 1){ done = 2; stopme3 = 2; } – DevOps