Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialNaya Willis
2,461 PointsHelp
Can somebody solve this one so I can review it to see what I was actually supposed to do. I felt like i was very close but not on point.
/* So the age old knock knock joke goes like this:
Person A: Knock Knock.
Person B: Who's there?
Person A: Banana
Person B: Banana who?
...as long as Person A has answered Banana the above repeats endlessly
...assuming the person answers Orange we'd see
Person B: Orange who?
...and then the punchline.
Person A: Orange you glad I didn't say Banana again?
(It's a really bad joke that makes it sound like "Aren't you glad I didn't say Banana again?")
Let's just assume the only two words passed in from the console from Person B are either banana or orange.
*/
// ====BEGIN PROMPTING CODE====
// Person A asks:
console.printf("Knock Knock.\n");
// Person B asks and Person A's response is stored in the String who:
String who = console.readLine("Who's there? ");
// Person B responds:
console.printf("%s who?\n", who);
// ==== END PROMPTING CODE ====
3 Answers
Axel McCode
13,869 PointsHere is code to solve the challenge. I added comments help you better understand it. Hope this helps :)
* So the age old knock knock joke goes like this:
Person A: Knock Knock.
Person B: Who's there?
Person A: Banana
Person B: Banana who?
...as long as Person A has answered Banana the above repeats endlessly
...assuming the person answers Orange we'd see
Person B: Orange who?
...and then the punchline.
Person A: Orange you glad I didn't say Banana again?
(It's a really bad joke that makes it sound like "Aren't you glad I didn't say Banana again?")
Let's just assume the only two words passed in from the console from Person B are either banana or orange.
*/
// ====BEGIN PROMPTING CODE====
// Person A asks:
console.printf("Knock Knock.\n");
// Person B asks and Person A's response is stored in the String who:
String who = ""; // created string outside the do-while loop so it is in scope of the loop
do{who = console.readLine("Who's there? "); // stores the string entered in the String variable "who"
}while(!who.equalsIgnoreCase("orange")); // checks if who is equal to "orange"
/*
*notice the not operator (!), this will make the conditional statement return the opposite of what it
*turns out to be. So if the String variable "who" is equal to "Orange", it would return true, however *because the "not" operator is there, it will return the opposite, which is false, in this case do-while
*loop will stop looping.
*/
// Person B responds:
console.printf("%s who?\n", who);
// ==== END PROMPTING CODE ====
Dustin Matlock
33,856 PointsHi Naya, see if this Google search is able to help resolve your issue.
Craig Dennis
Treehouse TeacherOr you can use who.equalsIgnoreCase("banana")
for the condition. We do not go over negation in Java Basics.
Naya Willis
2,461 PointsNaya Willis
2,461 PointsIt said to add the do-while loop. Then wrap it in the code block