2017-11-03 9 views
0

こんにちは皆、これはこのウェブサイトの私の最初の投稿であり、私はまだJavaを初めて使っています。この私のコードは私が取り組んでいる。このコードの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: "); 

ループし続けるということです。私はこのための解決策を考え出していない。適切なインデントで数分を投資

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

あなたの男の助けてくれてありがとう、私は私の問題の解決策を見つけました。 –