java - Predicate Method yes/no/maybe response -
i'm trying write program run method calls predicate method asks "do want go movie tonight?". if user enters "yes" question want program "ok. let's go tonight." if user enters "no" want program print "that's cool lets go next week." if user enters "maybe" want program "it's yes or no question" ask question again "do want go go movie tonight? " , wait user enter response again.
the problem i' having if user enters "maybe" program says "it's yes or no question" automatically prints "that's fine lets go next week." how fix incorrect logic in program? question in chapter focusing on parameter passing in book. did correctly design program pass string value run method isyesorno method i'm trying write?
import acm.program.*; public class moviestonight extends consoleprogram { public void run() { string answer = readline("do want go movie tonight?"); if (isyesorno(answer)) { println("ok. let's go tonight"); } else println("that's cool let's go next week"); } private boolean isyesorno(string response) { while (!response.equals("yes") && !response.equals("no")) { println("it's yes or no question"); break; } return (response.equals("yes")); } }
in addition suggestions provided, isyesorno
method contains significant error, in fact answer base question:
the problem i'm having if user enters "maybe" program says "it's yes or no question" automatically prints "that's fine lets go next week." how fix incorrect logic in program?
return (response.equals("yes"));
if response 'maybe', not equal 'yes', , return false
-- why prints, "that's cool let's go next week". in fact 'else' condition supplied if(isyesorno(answer))
.
as stands, you're checking see if response yes/no, starting while
loop runs if isn't yes/no, breaking while
loop prematurely, , returning false
on 1 of conditions spawned while
loop in first place (read: not 'yes'), gets handled 'no' (which may not case).
try following, if want use if-else:
public void askquestion(){ string response = readline("do want go movie tonight?"); getyesnoresponse(response); } public void getyesnoresponse(string answer){ if (answer.equals("yes"){ //print yes response } else if (answer.equals("no") { //print no response } else { askquestion(); } }
Comments
Post a Comment