Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

cant seem to get the number of guesses the user inputs. import java.util.*; public class GuessingGame { public static void main(String []args) { System.out.println(please enter

cant seem to get the number of guesses the user inputs.

import java.util.*; public class GuessingGame { public static void main(String []args) { System.out.println("please enter a high cap"); Scanner input = new Scanner(System.in);

int highnumber = input.nextInt(); int guesses = 0; int games = 1; int totalguesses = 0; int totguess = totalguesses; instructions(highnumber); game(highnumber, guesses, games, totalguesses); System.out.println("Do you want to play again? Enter yes to play again, or no to quit"); char gameagain = input.next().charAt(0); while (gameagain == 'y' || gameagain == 'Y') { game(highnumber, guesses, games, totalguesses); System.out.println("Do you want to play again? Enter yes to play again, or no to quit"); gameagain = input.next().charAt(0); games++; } if (gameagain =='n' || gameagain == 'N') { results(guesses, games, totguess); } } public static void instructions(int highnumber) { System.out.println("This program allows you to play a guessing game."); System.out.println("I will think of a number between 1 and " +highnumber); System.out.println("and will allow you to guess until you get it."); System.out.println("For each guess, I will tell you whether the "); System.out.println("right answer is high or lower than your guess."); } public static int game(int highnumber, int guesses, int games, int totalguesses) { System.out.println("I'm thinking of a number..."); Random x = new Random(); boolean flag = true; Scanner keyboard = new Scanner(System.in); int correct = x.nextInt(highnumber); guesses = 0; while(flag) { System.out.println("Your guess?"); int userinput = keyboard.nextInt(); if( userinput == correct) { System.out.println("You did it! good job"); flag = false; System.out.println("You guessed correctly after " +guesses+ " guesses"); } else if( userinput > correct) { System.out.println("lower"); guesses++; } else if(userinput < correct) { System.out.println("higher"); guesses++; } //guesses++; guesses+=totalguesses; } return totalguesses; } public static void results(int guesses, int games, int totguess) { System.out.println("Overall results: "); System.out.println("Total guesses : " +totguess); System.out.println("total games : " +games); System.out.println("guess/game : "); System.out.println("max guesses : "); } }

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

Modern Database Management

Authors: Heikki Topi, Jeffrey A Hoffer, Ramesh Venkataraman

13th Edition

0134773659, 978-0134773650

More Books

Students also viewed these Databases questions