anbarasu aladiyan wrote:Hi Steve Paj,
try like this
Thank you for your time and effort. I have to return true (when Y or y) and false (when N or n) to main.
Main is asking a user if they want to continue with the program. So they either answer Y or y for Yes or N or n for No. If they enter anything else it asks them again if they want to continue. They can only type Y or y or N or n.
While it's too late now, I want to remind our answerers that we strive *not* to provide ready-made solutions, but rather guide the people asking the question in a way they can figure it out for themselves. had I seen this earlier I would have deleted the answers.
I don't like all these continue and breaks and multiple returns. I would suggest you use a while loopThe while checks that you have neither Y nor N, and the single return statement looks to see whether it is "y". Using the literal before the . obviates the risk of input being null, which would cause a NullPointerException.You aren't doing anything in the loop, only using it as a check, so you must comment the empty statement. The extra () might be redundant.
Jesper Young wrote:I also don't like break, continue (especially with a label - that's an ugly goto in disguise) and infinite loops, but Campbell's code also looks a bit cryptic to me.
Jelle, the continue you have on line 10 is unnecessary.
Yep, somehow "Oooh, I know, a labeled continu will fix that!" popped into my brain yesterday, and in a moment of questionable clarity I noticed that the label was unnecessary, but somehow missed that the entire continu statement could be dropped. D'oh!
Joined: Oct 13, 2005
You're right, Jesper. I do have this ability to write cryptic code. Doing FORTH programming, of course, makes me even worse at that . How would you have written it? How about this?Although the dot operator . has a higher precedence than the bang operator !, I have used redundant () to underline that precedence.
My vote was for a do-while; in this case it seems like the most logical choice.
Either that or a Forth re-implementation, because Forth is awesome.
Joined: Nov 24, 2009
EDIT: Nevermind. I'm a newb and I really had to think about it for a while. All is clear now. Thank you for sharing these alternatives with me. You have truly enriched my learning!
Thank you all for your comments. That is some very pretty work you all have done there. I'm not sure if the last few iterations will work for me, because I have to return true for yes, but I also have to return false for no. Will these iterations accomplish that?
Our instructor wrote the main program and when he calls the method, main is expecting either a true or false, and based on that, of of two things will happen.
Joined: Oct 13, 2005
I haven't looked too closely at all the code submitted, but they will all (I believe) return true for "y"/"Y" and false for "n"/"N". Jelle's loop and mine will both repeat whenever you don't enter "y" or "n", so if you enter "maybe" it will simply repeat the prompt. If you enter "y" or "n" they test whether you have entered "y" (= true); the only other possibility at this stage is "n", which doesn't equal "y", so it returns false.