2014-01-26 100 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