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

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -