2016-11-13 61 views
-2

這是一個單詞搜索程序。它搜索的文本是輸入並轉換爲另一個類中的二維數組。Java:即使條件爲假,也執行循環執行

這是程序通過搜索文本:

10 //rows 
15 //columns 
fqexfecmxdvjlgu 
cxomfslieyitqtz 
nucatfakuxofegk 
hfytpnsdlhcorey 
pgrhdqsypyscped 
ckadhyudtioapje 
yerjodxnqzztfmf 
hypmmgoronkzhuo 
hdskymmpkzokaao 
amuewqvtmrlglad 

因爲即使我終止字符串end鍵入某些原因,它總是進入我的checkDown()方法,並創建一個出界失誤。如果我註釋掉這種方法,只執行checkRight()checkDiagonal()方法,一切似乎都可以正常工作。 這是我的代碼:

import java.util.Scanner; 

public class WordSearch 
{ 
    private char[][] array; 
    private String targetWord; 
    private int rowLocation; 
    private int colLocation; 

    public WordSearch(char[][] inArray) 
    { 
     array = inArray; 
    } 

    public void play() 
    { 
     do{ 
      for (int row = 0; row < array.length; row++) 
      { 
       for (int col = 0; col < array[row].length; col++) 
       { 
        System.out.print(array[row][col]); 
       } 
       System.out.println(); 
      } 

      System.out.println(); 
      Scanner input = new Scanner(System.in); 
      System.out.println("What word would you like to search for? Type end to quit: "); 
      targetWord = input.nextLine(); 
      System.out.println("Typed in: " + targetWord); 
      System.out.println(); 

      compareFirst(targetWord); 
     } while (!targetWord.equals("end")); 

    } 

    public void compareFirst(String inWord) 
    { 
     for (int row = 0; row < array.length; row++) 
     { 
      for (int col = 0; col < array[row].length; col++) 
      { 
       if(array[row][col] == inWord.charAt(0)) 
       { 

        rowLocation = row; 
        colLocation = col; 

        suspectAnalysis(); 
       } 
      } 
     } 
    } 

    public void suspectAnalysis() 
    { 
     checkRight(); 
     checkDown(); 
     checkDiagonal(); 
    } 


    public void checkRight() 
    { 
     for(int i = 1; i < (targetWord.length()); i++) 
     { 
      if(colLocation + i > array[0].length - 1) 
      { 
       return; 
      } 

      else if(array[rowLocation][colLocation + i] != targetWord.charAt(i)) 
      { 
       return; 
      } 
     } 
     System.out.println(targetWord + " found horizontally at row " + rowLocation + " and column " + colLocation); 
     System.out.println(); 

     return; 

    } 


    public void checkDown() 
    { 
     for(int i = 1; i < (targetWord.length()); i++) 
     { 
      if(rowLocation + i > array.length - 1 && colLocation + i > array[0].length - 1) 
      { 
       return; 
      } 
      else if(array[rowLocation + i][colLocation] != targetWord.charAt(i)) 
      { 
       return; 
      } 
     } 
     System.out.println(targetWord + " found vertically at row " + rowLocation + " and column " + colLocation); 
     System.out.println();   
    } 

    public void checkDiagonal() 
    { 
     for(int i = 1; i < (targetWord.length()); i++) 
     { 
      if(colLocation + i > array[0].length - 1 || rowLocation + i > array.length - 1) 
      { 
       return; 
      } 

      else if(array[rowLocation + i][colLocation + i] != targetWord.charAt(i)) 
      { 
       return; 
      } 
     } 
     System.out.println(targetWord + " found diagonally at row " + rowLocation + " and column " + colLocation); 
     System.out.println(); 
    } 
} 

爲什麼沒有這種情況發生的時候我註釋掉checkDown()方法?我該如何解決它?

我很感激任何幫助。謝謝!

+0

檢查您獲得索引超出綁定例外的語句。這種類型的異常不難理解和修復! –

+0

@Bluasul你可以添加你用來測試你的WordSearch的代碼嗎,你的WordSearch構造函數的示例參數是什麼? – alainlompo

+0

你的問題就像知道'do ... while'循環總是在測試while表達式中的條件之前執行循環體一樣簡單嗎?也就是說,你是否真的想要使用'while'循環(在決定是否執行循環體之前測試條件)? – dave

回答

0

有一個三種情況造成的失敗:

  1. 因爲你使用DO-while循環,它會檢查你的循環結束他的病情時,PROGRAMM正在尋找單詞「結束。完成

  2. 你的最後一排有一個「E」(和多數民衆贊成你的運氣;-))之前,因此,您的分析開始 與第9行,列3

  3. 此條件:

    如果(rowLocation + I> array.length - 1 & &搭配+ I>數組[0]。長度 - 1)

又名if(9 + 1 > 9 && 3 + 1 > 14)

還給真& &假=>假 導致你的下一個狀態的出反彈的:

else if(array[rowLocation + i][colLocation] != targetWord.charAt(i)) 

又名

else if(array[9 + 1][3] != targetWord.charAt(i)) 

因此改變& &到|| ...

如果你不慣於檢查該單詞 「結束」 了,而你的交換DO-而(真),並通過

if(!targetword.equals("end")) 
    break; 
檢查

緊隨掃描儀之後。

相關問題