有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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: "); 

我认为这个问题的原因是代码的这一部分。不过,我还没有想出解决这个问题的办法

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

     }//end chkFound

共 (1) 个答案