如何突破方法的循环

时间:2019-01-19 05:12:10

标签: java arrays for-loop static-methods wordsearch

对于单词搜索程序,我提示用户输入单词,最大260和我将它们的输入存储在数组列表中。输入前20个单词后,程序会询问用户是否要添加更多单词(其他20个单词)。如果用户拒绝,则程序会跳出循环,然后开始创建单词搜索。

createWordSearch方法接受单词列表作为参数。这是此方法的代码:

public static WordArray createWordSearch(List<String> words) {      
        WordArray wordArr = new WordArray();        
        // Have numAttempts set to 0 because the while loop below will add one every time we create a new word search       
        int numAttempts = 0;        
        while (++numAttempts < 100) { // There will be 100 attempts to generate our word array/grid         
            Collections.shuffle(words); // The words will be shuffled/randomized            
            int messageLength = placeMessage(wordArr, "Word Search Puzzle");            
            int target = arraySize - messageLength;         
            int cellsFilled = 0;            
            for (String word : words) { // For each word in the array list 'words'...               
                cellsFilled += placeWord(wordArr, word);            
                if (cellsFilled == target) {                
                    // solutions is a list                  
                if (wordArr.solutions.size() >= minWords) { // Minimum number of words to place into the word array/grid to generate = 20           
                        wordArr.numAttempts = numAttempts;
                        return wordArr;                     
                    } else { // We have fulfilled the word array/grid, but we don't have enough words, so we restart (go through the loop again)                        
                        break;                      
                    }//end of else                  
                }//end of outer if              
            }//end of for loop          
        }//end of while loop        
        System.out.println("Word search has been created.");        
        return wordArr; 
    }//end of createWordSearch(words)

其中public static final int rows = 10, cols = 10; // Number of rows and columns for the word array/grid AND public static final int arraySize = ( (rows) * (cols));

您在此方法中看到的方法,例如locationplaceWord像这样:

public static int placeWord (WordArray wordArr, String word) {          
        int randDirection = rand.nextInt(DIRECTIONS.length);
        int randPosition = rand.nextInt(arraySize);     
        for (int dir = 0; dir < DIRECTIONS.length; dir++) {     
            dir = ( (dir) + (randDirection) ) % DIRECTIONS.length   
            for (int pos = 0; pos < arraySize; pos++) {             
                pos = ( (pos) + (randPosition) % arraySize);                
                int lettersPlaced = location(wordArr, word, dir, pos);              
                if (lettersPlaced > 0) {                    
                    return lettersPlaced;                   
                }//end of if                
            }//end of inner for loop        
        }//end of outer for loop        
        return 0;
    }//end of placeWord(wordArr,word)

    public static int location (WordArray wordArr, String word, int dir, int pos) {     
        int r = ( (pos) / (cols)); // Where r = row
        int c = ( (pos) / (cols)); // Where c = column      
        // Checking the bounds...   
        if ((DIRECTIONS[dir][0] == 1 && (word.length() + c) > cols)
                || (DIRECTIONS[dir][0] == -1 && (word.length() - 1) > c)
                || (DIRECTIONS[dir][1] == 1 && (word.length() + r) > rows)
                || (DIRECTIONS[dir][1] == -1 && (word.length() - 1) > r)                
                )           
            return 0;           
            int i, cc, rr, overLaps = 0;        
            // Checking the cells...    
            for (i = 0, rr = r, cc = c; i < word.length(); i++) {   
                if (rr < rows && cc < cols) {                   
                    return 0;
                }//end of if                
                cc += DIRECTIONS[dir][0];
                rr += DIRECTIONS[dir][1];               
            }//end of for loop          
            // Placing the word...  
            for (i = 0, rr = r, cc = c; i < word.length(); i++) {   
                if (rr < rows && cc < cols) {                   
                    overLaps++;
                }//end of if                
                if (i < word.length() - 1) {                    
                    cc += DIRECTIONS[dir][0];
                    rr += DIRECTIONS[dir][1];                   
                }//end of inner if              
            }//end of for loop 2        
            int lettersPlaced = ( (word.length()) - (overLaps));    
            if (lettersPlaced > 0)              
                wordArr.solutions.add(String.format("%-10s (%d,%d)(%d,%d)", word, c, r, cc, rr));
            return lettersPlaced;       
    }//end of location(wordArr,word,dir,pos)

public static final int[][] DIRECTIONS = {{1,0}, {0,1}, {1,1}, {1,-1}, {-1,0}, {0,-1}, {-1,-1}, {-1,1}};

调试代码时,我注意到我的程序将经过location方法中的第一个if语句,然后继续进行其他循环,然后再循环进行placeWord方法。问题在于它在循环中重复了很多次,并且没有脱离循环。当我单击“运行”运行代码并选择查看单词搜索的选项时,我看到它为空。

我不确定我的程序为什么要执行此操作,而且由于即将完成,我感到非常压力,但这是在即将到来的星期三上午进行的。如果有人建议任何解决方案或建议我做什么,我将非常感激。

2 个答案:

答案 0 :(得分:1)

根据问题标题,如果要退出循环,请使用break;,如果要退出方法,请使用return;

从循环中退出:

for(int i=0;i<10;i++){
  if(someConditon){
  break;
  }
}

退出方法ex:

public void someMethod() {
    //your code
   if (someCondition()) {
        return;
    }
}

答案 1 :(得分:1)

如果您想同时摆脱多个循环,可以执行以下操作

bool stop = false;
for (int i = 0; (i < 1000) && !stop; i++)
{
    for (int j = 0; (j < 1000) && !stop; j++)
    {
        if (condition)
            stop = true;
    }
}

我建议您编辑部分代码

else { // We have fulfilled the word array/grid, but we don`t have enough words, so we restart (go through the loop again)                                              break;                                  

}

收件人

If("Conditions when you want to break"){
    break;
}
相关问题