2014-01-26 3 views
0

저는 작은 프로그램을 작성하고 있지만 while 루프는 작동하지 않는 것으로 보입니다. 인터넷을 통해 자습서를 찾아 보았지만 J를 늘려야한다는 것을 알고 있습니다. 내가 한 가지를 고칠 때 다른 부분이 작동을 멈추는 이유를 알아내는 것 같습니다! 누군가이 문제를 해결하기 위해 무엇을해야 할 지 말해 줄 수 있습니까?자바 루프가 작동하지 않습니다.

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

관련 문제