gpt4 book ai didi

java - 方法的重复

转载 作者:行者123 更新时间:2023-11-30 03:16:21 26 4
gpt4 key购买 nike

如果我想重复该方法,如果没有输入 yes 或 no,我该怎么做?我尝试过:

while (!answer.equals("yes") && !answer.equals("no")) {

但它只是一直输入“请回答是或否”,而没有提供再次写入的选项。

谢谢

import acm.program.*;

public class YesOrNoQuestion extends ConsoleProgram {
public void run() {
println("This program asks you whether you want instructions");
String answer = readLine("Type your answer here: ");
YesOrNo(answer);
}

private void YesOrNo(String answer) {
if (answer.equals("yes")) {
println("Alright here you go");
} if (answer.equals("no")) {
println("Alright fine");
} else println("Please type either yes, or no: ");

}
}

最佳答案

private void YesOrNo(String answer) {
if (answer.toLowerCase.equals("yes")) {
System.out.println("Alright here you go");
} else if (answer.toLowerCase.equals("no")) {
//↑ here!!!
System.out.println("Alright fine");
} else {
System.out.println("Please type either yes, or no: ");
run();
}

}

关于java - 方法的重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32499969/

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