How to get the value of totalguesses

Assignment Help JAVA Programming
Reference no: EM131578806

I just do not know how to get the value of "totalGuesses" and "bestGame" in the report value. I think there should be a return method but I did not know how to do that.

Please just use those skills that I used in this program, not include "do/while" and "static int" and so on...

*"Best game" means the least times to get the correct answer.
Thx.

import java.util.*;
public class Guess1 {
public static final int MAX = 100;
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
Random r = new Random();
int totalGames = 0;
int numGuesses = 0;
int totalGuesses = 0;
intro();
playGame(r, console, numGuesses, totalGuesses);
next(r, console, totalGames, numGuesses, totalGuesses);
}

public static void intro() {
System.out.println("This program allows you to play a guessing game.");
System.out.println("I will think of a number between 1 and " + MAX);
System.out.println( "and will allow you to guess until");
System.out.println("you get it. For each guess, I will tell you");
System.out.println("whether the right answer is higher or lower");
System.out.println("than your guess.");
}

public static void playGame(Random r, Scanner console, int numGuesses, int totalGuesses) {
System.out.println();
System.out.println("I am thinking of a number between 1 and " + MAX + "...");
int compNum = r.nextInt(MAX) + 1;
System.out.println(compNum);
int userNum = -1;
numGuesses = 0;
while (compNum != userNum) {
numGuesses++;
totalGuesses++;
System.out.print("Your guess? ");
userNum = console.nextInt();
if (compNum > userNum) {
System.out.println("It's higher.");
} else if (compNum < userNum) {
System.out.println("It's lower.");
}
}
if (numGuesses == 1) {
System.out.println("You got it right in 1 guess");
} else {
System.out.println("You got it right in " + numGuesses + " guesses");
}

}

public static void next(Random r, Scanner console, int totalGames, int numGuesses, int totalGuesses) {
System.out.print("Do you want to play again? ");
String answer = console.next();
totalGames = 1;
if (answer.startsWith("y") || answer.startsWith("Y")) {
playGame(r, console, numGuesses, totalGuesses);
next(r, console, totalGames, numGuesses, totalGuesses);
} else if (answer.startsWith("n") || answer.startsWith("N")) {
report(totalGames, totalGuesses);
}
}

public static void report(int totalGames, int totalGuesses) {
System.out.println();
System.out.println();
System.out.println("Overall results: ");
System.out.println(" total games = " + totalGames);
System.out.println(" total guesses = " + totalGuesses);
System.out.println(" guesses/game = ");
System.out.println(" best game = ");
}
}

Reference no: EM131578806

Questions Cloud

Changes in an employee''s values : Changes in an employee's values, interests, and job performance cause
Write a code of a gui : Write a code of a GUI. I choose a Contact list where you havea name, phone number, email and zipcode. The contact should be able to edit, save, delete
Develop a vulnerability assessment plan : For this assignment, you will need to develop a vulnerability assessment plan.
Two advantages and limitations of using historical cost : Explain two advantages and two limitations of using historical cost for the accounting valuation of assets; and the measurement of periodic income.
How to get the value of totalguesses : how to get the value of "totalGuesses" and "bestGame" in the report value. I think there should be a return method but I did not know how to do that.
Information technology professional prepared : 1. Are information technology professional prepared to be change agents? Should they be?
Implement community behavioral change : How can individual, group, and social efforts be combined to implement community behavioral change?
What is the probability that ith item is the first collision : When we hash n items into k locations, what is the probability that all n items hash to different locations?
Write a driver class that uses the computermicrobe class : You must create two Java files. One is called LastNameFirstNameWeek7Prog.java, and the other is called ComputerMicrobe.java.

Reviews

Write a Review

JAVA Programming Questions & Answers

  Create a program that begins by loading file onto arraylist

Using the attached catalog.txt file, create a program that begins by loading the file onto an arraylist of Album classes called Catalog. Each Album class consists of 3 attributes.

  Write a program that creates an enumeration representing

Sherri rolls a 3 and decides to continue. She then chooses to roll two more times (6, 1). Because she rolled a 1, Sherri's turn ends and she earns 0 points.

  Develop a thorough testing example with different values

If the rate of y is less than z, then the queue may never get full. The program will simulate process synchronization. You will want to make sure that you output a message during each of these transactions

  Java nearest neighbor algorithm

I need a java class named "Topology" that includes methods "NearestNeighbor" and "RandomNeighbor".

  Your technical leader has submitted additional requirements

a - your technical leader has submitted additional requirements for your application. you have been asked to enhance

  Write a generic linked list tree

Write a generic linked list tree and a generic linked list binary search tree class that inherits from your generic linked tree class

  String as input and returns the reverse of that string

Write a method that takes a String as input and returns the reverse of that String. Then write a second method that uses the first method to test whether or not an input String is a palindrome.

  Java application to generate three-digit random number

Write a Java application that generates a 3-digit random number 100 times. Display the output for each of the generated numbers and the sum of all 100 numbers.

  Create three jlabel and set their icons using the images

Write a Java programs which displays a frame that contains three labels. Each label displays a card, as shown in the figure below. Create three JLabel and set their icons using the images. Display three images from 54 image cards randomly

  Defined the class person to store the name of a person

The methods that we included merely set the name and print the name of a person. Redefine the class Person so that, in addition to what the existing class does

  Write the code to complete lines

Provide the application that helps users to calculate the area - what is static members in data type class and how to write static methods

  If there are 4 command line arguments

If there are 4 command line arguments (a b c filename.dat) then the engine should take a,b,c and store the resulting x1 and x2 in filename.dat working in silent mode (no use of screen)

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd