Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have a questions i was wondering if anyone could help me with the proper indendaion and spacing for this java program i did. import

I have a questions i was wondering if anyone could help me with the proper indendaion and spacing for this java program i did.

import java.util.Random;

import java.util.Scanner;

public class GuessingGame {

public static final int MAX = 100; public static void main(String[] args) { Scanner console = new Scanner(System.in); Random numberGenerator = new Random();

int count = 0; int totcount = 0; int gamesCount = 0; int tryGuess = 0;

int bestGuess = Integer.MAX_VALUE;

boolean gameEnd = false;

String choice;

gameIntro();

while (gameEnd == false) {

tryGuess = playGame(console, totcount, numberGenerator);

totcount += tryGuess;

if (tryGuess < bestGuess) {

bestGuess = tryGuess;

}

count++;

System.out.print("Do you want to play again? ");

console.nextLine();

choice = console.nextLine();

if (choice.toLowerCase().equals("n")) {

gameEnd = true;

}

}

gameStats(bestGuess, count, totcount);

}

public static void gameIntro() {

System.out.println("This program will allow you to play a guessing game.");

System.out.println("I will think of a number between 1 and 100");

System.out.println("and will allow you to guess until you get it");

System.out.println("For each guess, It will tell you whether the");

System.out.println("correct answer is higher or lower than your guess.");

}

public static int playGame(Scanner console, int totcount, Random numberGenerator) {

System.out.println();

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

int number = numberGenerator.nextInt(MAX) + 1;

Boolean gameEnd = false;

int numTries = 0;

while (gameEnd == false) {

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

int guess = console.nextInt();

numTries++;

totcount += numTries;

if (guess == number) {

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

gameEnd = true;

} else if (guess > number) {

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

} else {

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

}

}

return numTries;

}

public static void gameStats(int bestGuess, int count, int totcount) {

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

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

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

System.out.println(" guesses/game = " + roundNumber(totcount / count));

System.out.println(" best game = " + bestGuess);

}

public static double roundNumber(double number) {

return (Math.round(number * 10)) / 10.0;

}

}

also if you look at the code if i say no in the user input it still plays the game it only allows n and y but i want yes YES and no NO to be allowed also, so if anyone could help me add that part also thanks. this is the sample output btw

This program allows you to play a guessing game. It will think of a number between 1 and 100 and will allow you to guess until you get it. For each guess, It will tell you whether the correct answer is higher or lower than your guess. I'm thinking of a number between 1 and 100... Your guess? 50 It's lower. Your guess? 25 It's lower. Your guess? 12 It's lower. Your guess? 6 You got it right in 4 guesses Do you want to play again? y I'm thinking of a number between 1 and 100... Your guess? 50 It's lower. Your guess? 25 It's lower. Your guess? 12 It's higher. Your guess? 18 You got it right in 4 guesses Do you want to play again? YES I'm thinking of a number between 1 and 100... Your guess? 50 It's higher. Your guess? 75 It's lower. Your guess? 62 It's higher. Your guess? 68 It's lower. Your guess? 65 It's higher. Your guess? 66 It's higher. Your guess? 67 You got it right in 7 guesses Do you want to play again? nope Game statistics: total games = 3 total guesses = 15 guesses/game = 5.0 best game = 4

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Basics Computer EngineeringInformation Warehouse Basics From Science

Authors: Odiljon Jakbarov ,Anvarkhan Majidov

1st Edition

620675183X, 978-6206751830

More Books

Students also viewed these Databases questions