java - Boolean method with string returns -
in homework have predicate method prints question , waits question. if user enters no, method should return false, if user enters yes method should return true. have done ! in part have problems: if user enters thing program must "wrong answer" , repeat question. can't return string because boolean method , dont know how resolve this. thank you!!
import acm.program.consoleprogram; public class yesno extends consoleprogram{ public void run () { string answer = readline ("would instructions? "); println (strboo (answer)); } private boolean strboo(string answer){ if (answer.equals("yes")) { return true; } else if (answer.equals("no")) { return false; } else { return false; } } }
first strboo
poor method name. call getanswer()
, , use like,
private static boolean getanswer() { while (true) { string answerstr = readline ("would instructions? "); answerstr = (answerstr != null) ? answerstr.trim() : ""; if ("yes".equalsignorecase(answerstr)) { return true; } else if ("no".equalsignorecase(answerstr)) { return false; } else { system.out.println("wrong answer"); } } return false; }
Comments
Post a Comment