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 <= winnings) // compares the money won to amount at the begginning at the game
{
gamesWon++; // games won increses if you gain more money
gamesPlayed++;
}
else
{
gamesPlayed++;
}
// prompt user to play again
}
public static int printHeading (int money, int min, int max)
{
System.out.println("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
System.out.println("Welcome to the High-Low Betting Game");
System.out.println("You have $" + money + " to begin the game");
System.out.println("Valid guess are numbers between " + min + " and " + max);
System.out.println("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
return money;
}
public static int playGame (int money, int max, int min, double percentage)
{
/*Scanner input = new Scanner(System.in);
int bet = input.nextInt();*/
int guess = getGuess();
int bet = getBet(money);
int numDrawn = drawNum(max, min);
for (int num = 1; num <= 6; num++) // determines if guess is high, low, or correct
{
System.out.print("Guess " + num + ": " + guess);
System.out.println();
if (guess > numDrawn)
{
System.out.print("Too high...");
}
if (guess < numDrawn)
{
System.out.print("Too low...");
}
if (guess == numDrawn)
{
System.out.print("Correct!");
}
}
calcNewMoney(money, bet, guess);
playAgain(percentage);
return money;
}
/**
*
* @param money
* @return
*/
public static int getBet(int money)
{
System.out.println("Please enter a bet");
Scanner input = new Scanner(System.in);
int bet = input.nextInt();
if (bet < 1 || bet > 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((max - min) +1)+ min;
return randNum;
}
public static int calcNewMoney (int money, int bet, int guess)
{
int earnings = bet / guess;
if (guess <= 6)
{
money = money + earnings;
}
else
{
money = money - bet;
}
System.out.println("You have won $" + earnings);
System.out.println("You have $" + money);
return money;
}
public static boolean playAgain (double percentage)
{
boolean playAgain = true;
Scanner input = new Scanner(System.in);
System.out.println("You have won " + percentage + "% of the games played");
System.out.println("Would you like to play again? y or n");
String again = input.nextLine();
if (again.equalsIgnoreCase("y"))
{
playAgain = true;
}
else if (again.equalsIgnoreCase("n"))
{
playAgain = false;
}
return playAgain;
}
}
semi-colon
aka;
at the end of yourwhile
loop which just makes it not work as you would expect. \$\endgroup\$double percentage = (gamesWon / gamesPlayed) * 100;
\$\endgroup\$;
at the end of that while. \$\endgroup\$