Question

I wrote the following java code with the eclipse ide, which is supposed to report the...

I wrote the following java code with the eclipse ide, which is supposed to report the number of guesses it took to guess the right number between one and five. Can some repost the corrected code where the variable "guess" is incremented such that the correct number of guesses is displayed? please test run the code, not just look at it in case there are other bugs that exist.


import java.util.*;

public class GuessingGame {

   public static final int maxNUM = 5;

   public static void main(String[] args) {
       Scanner input = new Scanner(System.in);
       haiku();
       int guesses = playGame(input);
       System.out.print("Do you want to play again? ");
       String choice = input.next();
       char retry = choice.charAt(0);
       int game = 1;
       while (retry == 'Y' || retry == 'y') {
           playGame(input);
           game++;
           System.out.print("Do you want to play again? ");
           choice = input.next();
           retry = choice.charAt(0);
       }
       if (retry == 'N' || retry == 'n') {
           reportResults(input, guesses, game);
       }
       // int gamesPlayed = repeat(input);
       // reportResults(input, guesses, game);
   }

   public static int playGame(Scanner input) {
       Random r = new Random();
       int answer = r.nextInt(maxNUM) + 1;
       // System.out.println("secret number is " + answer);
       System.out.println("Guess a number between 1 and " + maxNUM + "...");
       int response = 0;
       int guess = 0;
       while (response != answer) {
           System.out.print("Your guess? ");
          
           response = input.nextInt();
           if (response > answer) {
              
               System.out.println("It's lower.");
              
           } else if (response < answer) {
          
               System.out.println("It's higher.");
              
           }
          
           if (response == answer) {
               if (guess == 1) {
                   System.out.println("Great Job! You got it right in 1 try");
               } else if (response == answer) {
                   System.out.println("You got it right in " + guess + " tries");
               }
           }
          
           for (int i = 1; i <= 1; i++) {
               guess = (guess * 10) % 10;
           }
           // System.out.println("guess = " + guess + " -1");
       }
       return guess - 1;
   }

   public static int repeat(Scanner input) {
       System.out.println();
       System.out.print("GAME OVER! Play Again?, (Y,N) ");
       String choice = input.next();
       char retry = choice.charAt(0);
       int game = 0;
       while (retry == 'Y' || retry == 'y') {
           playGame(input);
           System.out.print("PLAY AGAIN?, (Y/N) ");
           String nextTry = input.next();
           retry = nextTry.charAt(0);
           game++;
       }
       return game + 1;
   }

   // show overall results
   public static void reportResults(Scanner input, int guesses, int gamesPlayed) {
       System.out.println("Overall results:");
       System.out.println(" total number of games = " + gamesPlayed);
       System.out.println(" total guesses = " + guesses);
       System.out.println(" guesses/game = " + 1.0 * guesses / gamesPlayed);
       System.out.println(" best game = ");
   }

   public static double round1(double round) {
       return Math.round(round * 10.0) / 10.0;

   }

   // haiku
   public static void haiku() {
       System.out.println("You Guess the number.");
       System.out.println("Get it wrong and I'll say:");
       System.out.println("Higher or lower.");
       System.out.println();
   }
}

Homework Answers

Answer #1

copyable code

import java.util.*;

public class GuessGa {

//declare the integer varaibale mNUM

public static final int mNUM = 5;

      

//main()

     public static void main(String[] args) {

          Scanner in = new Scanner(System.in);

          Random rd = new Random();

          haiku();

          plyGame1(in,rd);

     }

    

     //haiku

     public static void haiku() {

          System.out.println("choose the number.");

      System.out.println("Guess a num and I'll say:");

      System.out.println("Higher or lower.");

      System.out.println();

     }

    

     //CHANGE THE CODE

     public static void plyGame1(Scanner in,Random rd) {

          int game = 1;

          int Guesses = Game(in,rd);

          int bestGame = Guesses;

          System.out.print("play again? ");

          String ans = in.next();

          System.out.println("\n");

          while (ans.charAt(0) == 'y' || ans.charAt(0) == 'Y') {

              int guess = Game(in,rd);

              Guesses += guess;

              game++;

              System.out.print(" play again? ");

              ans = in.next();

              System.out.println();

              if(guess < bestGame) {

                   bestGame = guess;

              }

          }

          reportResults(Guesses, game, bestGame);

     }

         

    

     public static int Game(Scanner in,Random rd) {

          System.out.println("I'm thinking of a num between 1 and " + mNUM + "...");

             int num = (rd.nextInt(mNUM) + 1);

          System.out.print("Your guess? ");

          int guess = in.nextInt();

          int response = 1;

          while (!(guess == num)) {

              if (guess > num) {

                    System.out.println("It's lower.");

              }

              if (guess < num) {

                   System.out.println("It's higher.");

              }

              System.out.print("Your guess? ");

              guess = in.nextInt();

              response++;

          }

          if (response == 1) {

              System.out.println("great job You got it right in 1 TRY!");

          } else {

              System.out.println("You got it right in " + response + " guesses!");

          }

          return response;

     }

    

     //show overall results

     public static void reportResults(int Guesses, int game, int bestGame) {

          double guesses_Game = (double)Guesses / game;

          System.out.println("Overall results:");

          System.out.println("total games   = " + game);

          System.out.println("total guesses = " + Guesses);

          System.out.print("Gueses/game   = ");

          System.out.printf("%.1f", guesses_Game);

          System.out.println("Best game     = " + bestGame);

     }

}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
THIS IS FOR JAVA I have to write a method for a game of Hangman. The...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The word the user is trying to guess is made up of hashtags like so " ###### " If the user guesses a letter correctly then that letter is revealed on the hashtags like so "##e##e##" If the user guesses incorrectly then it increments an int variable named count " ++count; " String guessWord(String guess,String word, String pound) In this method, you compare the character(letter)...
Refactor the following program to use ArrayList instead of Arrays. You can google "Java ArrayList" or...
Refactor the following program to use ArrayList instead of Arrays. You can google "Java ArrayList" or start with the link below: https://www.thoughtco.com/using-the-arraylist-2034204 import java.util.Scanner; public class DaysOfWeeks { public static void main(String[] args) { String DAY_OF_WEEKS[] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}; char ch; int n; Scanner scanner = new Scanner(System.in); do { System.out.print("Enter the day of the Week: "); n = scanner.nextInt() - 1; if (n >= 0 && n <= 6) System.out.println("The day of the week is " + DAY_OF_WEEKS[n] + ".");...
This is the code I have written for my Java homework assignment but I can't seem...
This is the code I have written for my Java homework assignment but I can't seem to get it to run. Any help would be appreciated! import javax.swing.JOptionPane; import java.io.*; import java.util.Scanner; public class javaGamev5 { public static void main(String[] args) throws IOException { String question = null, answerA = null, answerB = null, answerC = null ; int menuChoice = 0, correctAnswer = 0, points = 0, score = 0, highscore = 0; displayIntro(); do { menuChoice = displayMainMenu();...
In Java ReWrite the for loop program code example 3, above as a while loop. Code...
In Java ReWrite the for loop program code example 3, above as a while loop. Code Example 3 – Try it import java.util.Scanner; public static void main(String args[]) { int count = 0; int maxNumber = 0; int enteredNumber = -9999999; System.out.println("This program determines which entered number is the largest "); System.out.println("Enter count of numbers to check: "); Scanner scanIn = new Scanner(System.in); double count = scanIn.nextDouble(); scanIn.close(); // Print out message – input number – compare to see if...
Write a Java program that implements a lexical analyzer, lex, a recursive-descent parser, parse, an error...
Write a Java program that implements a lexical analyzer, lex, a recursive-descent parser, parse, an error handling program, error, and a code generator, codegen, for the following EBNF description of a simple assignment statement using an arithmetic expression language. Implement a symbol table by modifying Evaluate class. Modify the stmt() method, Provide an insert() and retrieve() method and store values in symbol table.   Video Help to get started:  https://www.youtube.com/watch?v=-C_S7Lb4kRk&feature=youtu.be import java.io.*; import java.util.*; public class parser { static String inString =...
I need to make some modifications to the code below. Numbers 1 through 3 are the...
I need to make some modifications to the code below. Numbers 1 through 3 are the required modifications. 1. Write a static method to have the players guess what was rolled 2. If guessed what was rolled you get 10 points 3. You win if you reach 30 points before the end of the round otherwise the person with the points wins at the end of five rounds The code is below import java.util.Scanner; public class ChoHan { public static...
Fix the program: what if the input is three? import java.util.Scanner public class Test { public...
Fix the program: what if the input is three? import java.util.Scanner public class Test { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter an integer: "); int m = in.nextInt(); System.out.print("Enter another integer: "); int n = in.nextInt(); System.out.println(m + " " + n); } }
Write a method that returns the sum of all the elements in a specified column in...
Write a method that returns the sum of all the elements in a specified column in a 3 x 4 matrix using the following header: public static double sumColumn(double[][] m, int columnIndex) The program should be broken down into methods, menu-driven, and check for proper input, etc. The problem I'm having is I'm trying to get my menu to execute the runProgram method. I'm not sure what should be in the parentheses to direct choice "1" to the method. I'm...
8.15 *zyLab: Method Library (Required & Human Graded) This code works but there are some problems...
8.15 *zyLab: Method Library (Required & Human Graded) This code works but there are some problems that need to be corrected. Your task is to complete it to course style and documentation standards CS 200 Style Guide. This project will be human graded. This class contains a set of methods. The main method contains some examples of using the methods. Figure out what each method does and style and document it appropriately. The display method is done for you and...
Covert the following Java program to a Python program: import java.util.Scanner; /** * Displays the average...
Covert the following Java program to a Python program: import java.util.Scanner; /** * Displays the average of a set of numbers */ public class AverageValue { public static void main(String[] args) { final int SENTINEL = 0; int newValue; int numValues = 0;                         int sumOfValues = 0; double avg; Scanner input = new Scanner(System.in); /* Get a set of numbers from user */ System.out.println("Calculate Average Program"); System.out.print("Enter a value (" + SENTINEL + " to quit): "); newValue =...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT