GuessingGame.java 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /**
  2. * Too Large Too Small - a number guessing game
  3. *
  4. * @ShakilaM
  5. * @version 0.1, MAy 2018
  6. */
  7. import java.util.*;
  8. import java.lang.Math;
  9. //Class
  10. public class GuessingGame
  11. {
  12. // instance variables - replace the example below with your own
  13. //randomly generated number
  14. private int answer;
  15. private int counter;
  16. private int wrong;
  17. //Constructor (name always same as class, no return req)
  18. public GuessingGame(int min, int max){
  19. min = 1;
  20. max = 10;
  21. answer = (int)((Math.random() * ((max-min) + 1)) + min);
  22. //counter = wrong;
  23. } //end Guessing Game
  24. public int wrongAnswers(int counter){
  25. counter = wrong;
  26. return counter;
  27. }//end WrongAnswers
  28. public void playGame(){
  29. //call game method
  30. }//end PlayGame
  31. public void doYouWantToPlay(){
  32. Scanner answer = new Scanner(System.in);
  33. System.out.print("Do you want to play a game?");
  34. String response = answer.nextLine();
  35. if(response.equalsIgnoreCase("no")){
  36. System.out.println("Well, bye then...");
  37. //how to close/quit?
  38. }
  39. else {
  40. System.out.println("Okay, pick a number between 1 and 10...");
  41. randomNumber();
  42. }
  43. }
  44. public void randomNumber(){
  45. Scanner guess = new Scanner (System.in);
  46. int response = guess.nextInt();
  47. int old = 0;
  48. do{
  49. if (response == old){
  50. System.out.println("Same same!");
  51. old = response;
  52. response = guess.nextInt();
  53. }
  54. /**else if(response==answer){
  55. System.out.println("Great job! You're so good at this! Feel free to play again...");
  56. break;
  57. }*/
  58. else if(response > answer){
  59. System.out.println("That's too high! Try again...");
  60. wrong = wrong + 1;
  61. old = response;
  62. response = guess.nextInt();
  63. }
  64. else{
  65. System.out.println("Too low. Give it another try...");
  66. wrong = wrong + 1;
  67. old = response;
  68. response = guess.nextInt();
  69. }
  70. }
  71. while (response != answer) ;
  72. wrong = wrong + 1;
  73. old = 0;
  74. System.out.println("Great job! You're so good at this! Feel free to play again.");
  75. System.out.println("It took you " + wrongAnswers(counter) + " tries!");
  76. counter = 0;
  77. doYouWantToPlay();
  78. }//end randomNumber
  79. }//end Class