2017-11-03 4 views
0

안녕하세요,이 사람은이 웹 사이트의 첫 번째 게시물이며 아직 자바를 처음 사용합니다. 이 코드는 내가 작업 중입니다. 이 코드에 대한Java의 메소드에서 루핑 오류

public static void main(String[] args) throws Exception { 
    // debug 
    if ($DEBUG) System.out.println("starting\n"); 
    //read data from text file into arrays w,p 
    String[] wArr = new String[50]; 
    String[] pArr = new String[50]; 
    String fileName = "homs.txt"; 
    readFile(fileName, wArr, pArr); 
//main control loop 
    while (true) { 
    //use input dialog to get 2 words from user 
    String input = JOptionPane.showInputDialog(null,"Enter two words: "); 
    String[] words = input.split("\\s+"); 
    String w1 = words[0]; 
    String w2 = words[1]; 
    //check each word if in dictionary 
    int w1ix = chkFound(wArr, w1);  
    boolean isFound = (w1ix >= 0); 
    System.out.println(w1 + " is found: " + isFound); 
    int w2ix = chkFound(wArr, w2); 
    boolean isFound2 = (w2ix >= 0); 
    System.out.println(w2 + " is found: " + isFound2); 
     if (w1ix >=0 && w2ix >=0) msg = "both words " + w1 + " and " + w2 + 
     "\n\tare in dictionary"; 
     else { msg = "one or more words not in dictionary: "; 
      if (w1ix <0) msg += w1 + " "; 
      if (w2ix <0) msg += w2 + " "; 
     System.out.println(msg); 
    //check if homonyms 
    boolean isHom = chkHom(pArr, w1, w2); 
    //output result 
    String line = msg + 
    "\nWord 1: " + w1 + 
    "\nWord 2: " + w2 + 
    "\nWord 1 in dictionary: " + isFound + 
    "\nWord 2 in dictionary: " + isFound2 + 
    "\nHomonyms: " + isHom; 

    JOptionPane.showMessageDialog(null, line); 
    //ask user to continue Y/N? 
    int cont = JOptionPane.showConfirmDialog(null, "Continue?"); 
    if (cont > 0) 
    break;//exit loop or continue 
    } 
//end main 
} 
} 

    public static int chkFound(String[] wArr, String w) { 
    for (String a : wArr) {  
     if(a.equals(w)) 
      return 1;    
     }     
      return -1; 

    }//end chkFound 

내 문제는 내가 그것을 실행할 때 나는이 문제에 대한 이유 코드의 일부라고 생각

String input = JOptionPane.showInputDialog(null,"Enter two words: "); 

반복 유지한다는 것이다. 나는 이것을위한 해결책을 생각해 내지 못했다. 적절한 identation에 몇 분을 투자

final int cont = JOptionPane.showConfirmDialog(null, "Continue?","Continue?", JOptionPane.YES_NO_OPTION); 
if(cont == JOptionPane.NO_OPTION){ 
break; 
} 
+1

Conside :

public static int chkFound(String[] wArr, String w) { for (String a : wArr) { if(a.equals(w)) return 1; } return -1; }//end chkFound 
profimedica

답변

0

https://docs.oracle.com/javase/7/docs/api/constant-values.html#javax.swing.JOptionPane.OK_OPTION

public static final int  OK_OPTION 0 

당신의 휴식을

if (cont > 0) 
    break;//exit loop or continue 

변화를 작동하지 않습니다. if-else에 괄호를 사용하십시오. System.out.println을 사용하여 변수 값을 확인하십시오.

+0

맞아요. 이건 버그입니다.하지만 이것은 OP 코드가있는 또 다른 버그입니다. – Makoto

+0

당신의 남자 도움을 주셔서 감사합니다 내 문제에 대한 해결책을 찾았습니다. –

관련 문제