Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Lab 3: Fortune Teller
- *
- * In this lab, you'll be implementing a fortune teller, which answers yes/no
- * questions with a randomly-selected response from a pre-generated list.
- *
- * This lab is a follow-along lab: the TA will introduce each part, give you a
- * few minutes to figure it out on your own, then go over the solution before
- * moving on to the next part. You still must have a working solution at the end
- * of lab to get credit, but you should be able to get there by following along.
- *
- * This lab is intentionally short, so use the extra time to ask questions of
- * your TA to make sure you really understand arrays and how they work.
- */
- import java.util.Random; // needed for random number generation
- import java.util.Scanner; // needed for scanner
- public class Lab3 {
- public static void main(String[] args) {
- /*
- * Task 1: statically declare an array of Strings to hold the responses. You
- * should use the static array initializer syntax we talked about in class.
- *
- * Some example responses are "Yes", "No", "Maybe", "Ask again later",
- * "Definitely", and "Definitely not", but feel free to come up with your
- * own.
- */
- String [] fortunes = {"Yes", "No", "Maybe", "Ask again later"};
- // declare a random number generator called rng, and a keyboard scanner
- // called kbd.
- Random rng = new Random();
- Scanner kbd = new Scanner(System.in);
- // Declare a String to hold the user's answer to "do you want to ask again?"
- String try_again;
- do {
- // Tell the user to ask their question. Feel free to customize this text
- // to give it some more personal flair.
- System.out.print("Ask a yes/no question: ");
- kbd.nextLine(); // read a line of text from the user. don't store it, because we don't need to.
- /*
- * Task 2: print a random response from your array of responses.
- * You can do this by generating a random number (using rng.nextInt(...)),
- * then printing out the response at that index.
- */
- int n = rng.nextInt(fortunes.length);
- System.out.println(fortunes[n]);
- // ask the user if they want to try again
- System.out.print("Try again (y/n)? ");
- // read in the user's response
- try_again = kbd.nextLine();
- } while (try_again.equalsIgnoreCase("y"));
- // print out a goodbye message
- System.out.println("I hope you got the answers you were looking for!");
- System.out.println("By the way, here were all the possible responses: ");
- /*
- * Task 3: print out all the possible responses from your array. This
- * probably involves a for loop.
- */
- for(int i = 0; i < fortunes.length; i++){
- System.out.println(fortunes[i]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement