当一个条件调用此函数时如何在 Java 中打破 while (true) 循环

问题描述 投票:0回答:0

下面是函数中的一个while循环。如果只绘制一张图片,它可以正常工作。当绘制了多张图片时,它会一直询问 Y/N。例如3张图片,必须回答N次3次才能打破这个循环,而无论图片数量多少,只要回答一次Y就会打破这个循环。

while (true) {
    System.out.println("Draw another picture (Y/N)?");
    String command = userInput.nextLine();
    if (command.equalsIgnoreCase("Y")) {
        drawPictures());
    } else if (command.equalsIgnoreCase("N")) {
        break;
    } else {
        System.out.println("Unsupported option.");
    }
}

我尝试用return/function to be called来代替break。但这个问题仍然存在。如果有人能教我如何打破这个循环,无论图片的数量如何,只要 N 的一个输入,我真的很感激。

java while-loop break terminate
© www.soinside.com 2019 - 2024. All rights reserved.