2014-01-26 98 views
0

因此,我正在編寫一個小程序,但我的while循環似乎不工作,我看遍了互聯網的教程,我知道我需要增加J,但我不能似乎弄清楚在哪裏,因爲當我得到一件事固定另一部分停止工作! 有人能告訴我我需要做什麼來解決這個問題嗎?Java循環不工作

int j=0;//which group we're checking 
      while(response==0){ 
       { 
        if(inArray(quote.toLowerCase(),greetings[j*2])) 
        { 
         response=2; 
         int r=(int)Math.floor(Math.random()*greetings[(j*2)+1].length); 
         addText("\n-->Miku:\t"+greetings[(j*2)+1][r]); 
         try (BufferedReader br = new BufferedReader(new FileReader("mikuname.txt"))) { 
          String name; 
          while ((name = br.readLine()) != null) { 
          addText(name +"!"); 
          } 
         } catch (IOException e1) { 
          // Do something with the IO problem that occurred while reading the file 
         } 
        } 
       } 
       if(response==0) 
       { 
        if(inArray(quote.toLowerCase(),chatBot[j*2])) 
        { 
         response=2; 
         int r=(int)Math.floor(Math.random()*chatBot[(j*2)+1].length); 
         addText("\n-->Miku:\t"+chatBot[(j*2)+1][r]); 
        } 
       } 
       if(response==0) 
       { 
        response=1; 
       } 
      } 
+1

爲什麼你在'while'之後有兩個'{'? – Maroun

+0

你打算用這個while循環完成什麼? – Mike

+0

我在我的代碼中修復一個錯誤的方式是使用我的調試器。我建議你逐步調試調試器中的代碼,看看它爲什麼在做它正在做的事情。 –

回答

0

你可能想要做的是在每次迭代中遞增j。我沒有看過你的邏輯或試圖理解你想要完成什麼,但是加入j++會使它在循環的每次迭代中增加1。否則j*2將始終爲0;

int j=0;//which group we're checking 
     while(response==0){ 
      { 
       j++; 
       if(inArray(quote.toLowerCase(),greetings[j*2])) 
       { 
        response=2; 
        int r=(int)Math.floor(Math.random()*greetings[(j*2)+1].length); 
        addText("\n-->Miku:\t"+greetings[(j*2)+1][r]); 
        try (BufferedReader br = new BufferedReader(new FileReader("mikuname.txt"))) { 
         String name; 
         while ((name = br.readLine()) != null) { 
         addText(name +"!"); 
         } 
        } catch (IOException e1) { 
         // Do something with the IO problem that occurred while reading the file 
        } 
       } 
      } 
      if(response==0) 
      { 
       if(inArray(quote.toLowerCase(),chatBot[j*2])) 
       { 
        response=2; 
        int r=(int)Math.floor(Math.random()*chatBot[(j*2)+1].length); 
        addText("\n-->Miku:\t"+chatBot[(j*2)+1][r]); 
       } 
      } 
     } 
+1

那是什麼?... – Maroun

+0

他的代碼加上j ++。 – Mike

+1

你應該解釋你的答案,以便OP知道他的錯誤。 – Maroun