簡體   English   中英

Java如何使用用戶的布爾輸入來結束do-while循環?

[英]Java how to end a do-while loop using boolean input from user?

因此,我想讓用戶輸入“ true”或“ false”退出循環。 就像我現在所擁有的那樣,無論我鍵入什么內容,它都會退出循環。

public static void main(String[] args) {
    // Color guard flags
    Scanner kb = new Scanner(System.in);
    Flag yourFlag;
    double weight;
    boolean correct;
    do {
        System.out.println("Hello!\nWhat colors are your silk? (most common two)\nHow long is your pole (in feet)?"
                + "\nWhat have you named your flag?\nHow heavy is your flag (in pounds)?");
        yourFlag = new Flag(kb.next(), kb.next(), kb.nextInt(), kb.next(), kb.nextDouble());
        if (yourFlag.getWeight() >= 7) {
            System.out.println("That sounds like a cool flag! It sounds pretty heavy though.");
        } else {
            System.out.println("That sounds like a cool flag!");
        }
        System.out.println("You've entered that your flag " + yourFlag.getName() + " is " + yourFlag.getWeight() + " pounds and "
                + yourFlag.lengthOfPole + " feet long. It also has the pretty colors " + yourFlag.silkColor1 + " and " + yourFlag.silkColor2);
        System.out.println("Is that correct? (True or flase)");
        correct = kb.nextBoolean();
    }
    while (correct = false);

順便說一句,是的,這是一個有關顏色保護的程序

correct = false是一項分配。

您應該使用!correct來測試correctfalse

示例代碼;

do {
    //
}
while(!correct);
//while(correct == false)

如果你想要的話

do {
    //
}
while(correct);
//while(correct == true)

暫無
暫無

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

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