HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaModerate

High - low number guessing game

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
numberlowhighgameguessing

Problem

I'm learning how to code in Java and I decided to re-write a program made in C++ into Java. All feedback is welcome.

Here is the original prompt:


This program generates a pseudo-random number between 1 and 100,
inclusive. The user will then be presented with opportunities to guess
that number. After each guess, the program indicates whether the guess
was too high, too low, or correct. The user will bet on each number.
Initially, the program will give the user $1000. Each round,the
program prompts the user for a bet. The program then prompts for
guesses until the user is correct, or has made 6 wrong guesses. Once
the round has ended, either by a correct guess or by using up the 6
guesses, the program displays the current status and prompts the user
to see if he/she wants to play again. This will continue until the
user indicates that they do not want to play again or until the player
runs out of money.

```
package high_low_game;
import java.util.*;
/**
*
* @author mjoseph2017
*/
public class High_low_game {

public static void main(String[] args)
{
int money = 1000;
int min = 1;
int max = 100;
int gamesWon = 0;
int gamesPlayed = 1;
boolean playAgain = true;
double percentage = (gamesWon / gamesPlayed) * 100;

while (playAgain == true)
{
printHeading(money, min, max);
playGame(money, max, min, percentage);
}

double winnings;
winnings = playGame (money, max, min, percentage);

if (money numDrawn)
{
System.out.print("Too high...");
}

if (guess 100)
{
System.out.println("That is not a valid bet");
getBet(money);
}

return bet;
}

public static int getGuess ()
{
Scanner input = new Scanner(System.in);
System.out.println("Please enter a guess");
int guess = input.nextInt();
return guess;
}

public static int drawNum (int max, int min)
{
Random rand = new Random();
int randNum = rand.nextInt((

Solution

In the first place, you have this code:

if (money <= winnings) // compares the money won to amount at the beginning at the game
{
    gamesWon++; // games won increases if you gain more money
    gamesPlayed++;
}

else 
{
    gamesPlayed++;
}


This is redundant. You should do this instead, which also shows what happens better:

gamesPlayed++; // increment gamesPlayed

if (money <= winnings) // increment gamesWon if winnings is <= money
{
    gamesWon++; // increment gamesWon
}


Additionally, your code formatting is awful. You should really format your code so all the levels of indentation are indented the same amount; this will really increase readability.

Code Snippets

if (money <= winnings) // compares the money won to amount at the beginning at the game
{
    gamesWon++; // games won increases if you gain more money
    gamesPlayed++;
}

else 
{
    gamesPlayed++;
}
gamesPlayed++; // increment gamesPlayed

if (money <= winnings) // increment gamesWon if winnings is <= money
{
    gamesWon++; // increment gamesWon
}

Context

StackExchange Code Review Q#69605, answer score: 10

Revisions (0)

No revisions yet.