Last active
October 19, 2023 17:27
-
-
Save gigamonkey/a6a7145ce80b31a5fc817f282f0f13fb to your computer and use it in GitHub Desktop.
Starter code for a chatbot.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
/** | |
* Make a chatbot. See [1] for some background on chatbots. But write your code | |
* here in a Codespace, not in Replit. | |
* | |
* [1] https://runestone.academy/ns/books/published/BHSawesome/Unit3-If-Statements/magpieindex.html | |
*/ | |
public class Chatbot { | |
// As in TwentyQuestions, use input.readLine() to read a line at a time as a | |
// String from the user. | |
private Scanner input = new Scanner(System.in); | |
public void chat() { | |
say(hello()); | |
while (true) { | |
String line = input.nextLine(); | |
if (line.equals("quit") || line.equals("bye")) { | |
break; // this will break us out of the while loop | |
} | |
// TODO 1: figure out what the user is saying. | |
// TODO 2: Say something in response. | |
} | |
say(goodbye()); | |
} | |
private void say(String s) { | |
System.out.println(s); | |
} | |
private String hello() { | |
return "Hello. What would you like to talk about?"; | |
} | |
private String goodbye() { | |
return "Okay, bye! It's been nice chatting with you."; | |
} | |
public static void main(String[] args) { | |
new Chatbot().chat(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment