簡體   English   中英

在退出包含在Java字符串中的while循環時需要幫助

[英]Need assistance with exiting this while loop that is contained within a string in java

我嘗試放置兩個用戶輸入,但是在執行二進制搜索以找到數組列表中的項之后,似乎找不到退出此循環的方法。 我需要它返回一個字符串,以將輸入用作我的游戲的名稱。 到目前為止,這是我得到的:

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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM