gpt4 book ai didi

java - 需要帮助退出包含在 java 字符串中的 while 循环

转载 作者:行者123 更新时间:2023-12-02 09:58:10 25 4
gpt4 key购买 nike

我尝试过输入两个用户输入,但在进行二分搜索以在数组列表中查找项目后似乎找不到退出此循环的方法。我需要它返回一个字符串以使用输入作为我的游戏的名称。这是我到目前为止所得到的:

public String chooseBird() { //bird chosen will be depending on user input and binary search
String chosenBird = "";

String[] tempArr = new String[myBirdList.size()];//breks birds into array list string
tempArr = myBirdList.toArray(tempArr);
String selectBird ;
System.out.println("Enter the bird you wish to use for the adventure: ");
Scanner mySearch = new Scanner(System.in); //assign my search as
selectBird = mySearch.nextLine();

System.out.println("Hit 'begin' to start or 'end' to stop");
Scanner myInput = new Scanner(System.in);
String myValue = "";
int result = binarySearch(tempArr, selectBird);

do{
myValue = myInput.nextLine();
if (result == -1) {
System.out.println("We could not find this bird, please enter a bird from the list above");
}
else {
chosenBird = selectBird; //chosenBird is assigned as the return value to assign as the game name
System.out.println("\n" + selectBird + " was chosen and found at index " + result); //bird has been found in the list and selected for the adventure
}
} while (!myValue.contains("end") || result != -1); //keep prompting for input until we find a bird, bird found when result does not = -1

return chosenBird;
}

最佳答案

您需要为此使用 break 语句:

chosenBird = selectBird; //chosenBird is assigned as the return value to assign as the game name
System.out.println("\n" + selectBird + " was chosen and found at index " + result); //bird has been found in the list and selected for the adventure
break;

关于java - 需要帮助退出包含在 java 字符串中的 while 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55841103/

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