Advertisement
Guest User

Untitled

a guest
Feb 16th, 2020
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. /*
  2. * Lab 3: Fortune Teller
  3. *
  4. * In this lab, you'll be implementing a fortune teller, which answers yes/no
  5. * questions with a randomly-selected response from a pre-generated list.
  6. *
  7. * This lab is a follow-along lab: the TA will introduce each part, give you a
  8. * few minutes to figure it out on your own, then go over the solution before
  9. * moving on to the next part. You still must have a working solution at the end
  10. * of lab to get credit, but you should be able to get there by following along.
  11. *
  12. * This lab is intentionally short, so use the extra time to ask questions of
  13. * your TA to make sure you really understand arrays and how they work.
  14. */
  15.  
  16. import java.util.Random; // needed for random number generation
  17. import java.util.Scanner; // needed for scanner
  18.  
  19. public class Lab3 {
  20.  
  21. public static void main(String[] args) {
  22.  
  23. /*
  24. * Task 1: statically declare an array of Strings to hold the responses. You
  25. * should use the static array initializer syntax we talked about in class.
  26. *
  27. * Some example responses are "Yes", "No", "Maybe", "Ask again later",
  28. * "Definitely", and "Definitely not", but feel free to come up with your
  29. * own.
  30. */
  31. String [] fortunes = {"Yes", "No", "Maybe", "Ask again later"};
  32.  
  33. // declare a random number generator called rng, and a keyboard scanner
  34. // called kbd.
  35. Random rng = new Random();
  36. Scanner kbd = new Scanner(System.in);
  37.  
  38. // Declare a String to hold the user's answer to "do you want to ask again?"
  39. String try_again;
  40.  
  41. do {
  42.  
  43. // Tell the user to ask their question. Feel free to customize this text
  44. // to give it some more personal flair.
  45. System.out.print("Ask a yes/no question: ");
  46.  
  47. kbd.nextLine(); // read a line of text from the user. don't store it, because we don't need to.
  48.  
  49. /*
  50. * Task 2: print a random response from your array of responses.
  51. * You can do this by generating a random number (using rng.nextInt(...)),
  52. * then printing out the response at that index.
  53. */
  54. int n = rng.nextInt(fortunes.length);
  55. System.out.println(fortunes[n]);
  56.  
  57.  
  58. // ask the user if they want to try again
  59. System.out.print("Try again (y/n)? ");
  60.  
  61. // read in the user's response
  62. try_again = kbd.nextLine();
  63. } while (try_again.equalsIgnoreCase("y"));
  64.  
  65. // print out a goodbye message
  66. System.out.println("I hope you got the answers you were looking for!");
  67.  
  68. System.out.println("By the way, here were all the possible responses: ");
  69.  
  70. /*
  71. * Task 3: print out all the possible responses from your array. This
  72. * probably involves a for loop.
  73. */
  74. for(int i = 0; i < fortunes.length; i++){
  75. System.out.println(fortunes[i]);
  76. }
  77.  
  78. }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement