java - 方法的重复

标签 java methods while-loop infinite-loop predicate

如果我想重复该方法,如果没有输入 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/

相关文章:

python - @staticmethod 和@classmethod 的区别

c - 以用户给出的一位数字打印所有自恋数字

java - Jackson 库 1.x 数组如果一个元素有时只是一个字符串

java - 使用多个排序键查询 Dynamodb

java - 锁和同步方法的区别

ios - 想从 appdelegate 调用 ViewController 的方法

java - 无限循环;不明白怎么办

java - 处理 JSP 时发生异常 - while 循环

java - 原木锻造强化修复

java - 如何在 Java BlackBerry 中将编码的 jpeg 图像保存到文件