gpt4 book ai didi

java - 如何打破方法中的循环

转载 作者:行者123 更新时间:2023-12-02 01:42:27 25 4
gpt4 key购买 nike

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

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;//单词数组/网格的行数和列数 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 方法中循环。问题在于它在循环中重复了很多次,并且没有跳出循环。当我单击“运行”来运行代码并选择查看单词搜索的选项时,我发现它是空的。

我不知道为什么我的程序要这样做,而且我压力很大,因为我快要完成了,但它的截止时间是在即将到来的周三早上。如果有人推荐任何解决方案或建议我该怎么做,我将非常感激。

最佳答案

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

跳出循环例如:

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

退出方法 ex:

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

关于java - 如何打破方法中的循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54264253/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com