I'm doing an assignment for my programming class, but something is wrong with my code (again) and I've spent the past hour and a half trying to debug it. The code is a sort of gambling game, where the user enters an amount to bet, and chooses either high, low, or sevens. high is when the die roll an 8 or higher, low is when the die roll a 6 or lower, and seven is when the die roll a total of 7. If the die rolls seven, the bet is multiplied by 4 and awarded to the user. Otherwise, when the user "loses", they lose the amount that they bet. I can't seem to figure out exactly what is wrong.
To begin with, the winnings are not correct. They are showing up in the console as "You won 128 dollars!" and then when the code repeats,the current pool always becomes 132, even when the user lost and money should have been subtracted. Also, the program skips the "if" statements in the determineWinnings method - it defaults to the else even when the user should have won. This is due in 20 minutes and I can't figure out how to fix it! Any help is sincerely appreciated!
package example;
import java.util.Scanner;
public class test
{
public static void main(String[] args)
{
Scanner inScanner = new Scanner(System.in);
int currentPool = 100;
int bet = ' ';
char highLow = ' ';
int roll = ' ';
int winnings = ' ';
System.out.println("You have " + currentPool + " dollars.");
getBet(inScanner, currentPool);
getHighLow(inScanner);
determineWinnings(highLow, bet, roll);
currentPool = currentPool + winnings;
System.out.println("");
while (bet != 0)
{
System.out.println("You have " + currentPool + " dollars.");
getBet(inScanner, currentPool);
getHighLow(inScanner);
determineWinnings(highLow, bet, roll);
currentPool = currentPool + winnings;
System.out.println("");
}
}
private static int getBet(Scanner inScanner, int currentPool)
{
System.out.print("Enter an amount to bet (0 to quit): ");
String strBet = inScanner.nextLine();
int bet = Integer.parseInt(strBet);
while (bet < 0 || bet > currentPool)
{
System.out.print("Your bet MUST be between 0 and " + currentPool + " dollars.");
System.out.println("You have " + currentPool + " dollars.");
System.out.print("Enter an amount to bet (0 to quit): ");
strBet = (inScanner.nextLine());
bet = Integer.parseInt(strBet);
}
if (bet == 0)
{
System.out.println("You have " + currentPool + " dollars.");
System.out.println("Goodbye!");
return bet;
}
else
{
return bet;
}
}
private static char getHighLow(Scanner inScanner)
{
System.out.print("High, Low, or Sevens (H/L/S): ");
String hls = inScanner.nextLine();
char highLow = ' ';
if (hls.equals("H") || hls.equals("h"))
{
highLow = 'H';
}
else if (hls.equals("L") || hls.equals("l"))
{
highLow = 'L';
}
else if (hls.equals("S") || hls.equals("s"))
{
highLow = 'S';
}
else
{
System.out.print("ERROR: invalid character entered. Please try again.");
while (!hls.equals("H") || !hls.equals("h") || !hls.equals("L") || !hls.equals("l") || !hls.equals("S") || !hls.equals("s"))
{
System.out.println("High, Low, or Sevens (H/L/S): ");
hls = inScanner.nextLine();
}
}
return highLow;
}
private static int getRoll()
{
int roll = (int)Math.floor(Math.random() * 6 + 1);
return roll;
}
private static int determineWinnings(char highLow, int bet, int roll)
{
int rollOne = getRoll();
int rollTwo = getRoll();
int total = rollOne + rollTwo;
int winnings = bet + 0;
System.out.println("Die 1 rolls: " + rollOne);
System.out.println("Die 2 rolls: " + rollTwo);
System.out.println("Total of two dice is: " + total);
if (highLow == 'H')
{
if (total >= 8)
{
System.out.println("You won " + winnings + " dollars!");
}
else
{
System.out.println("You lost!");
winnings = 0 - bet;
}
}
else if (highLow == 'L')
{
if (total <= 6)
{
System.out.println("You won " + winnings + " dollars!");
}
else
{
System.out.println("You lost!");
winnings = (0 - bet);
}
}
else
{
if (total == 7)
{
winnings = bet * 4;
System.out.println("You won " + winnings + " dollars!");
}
else
{
System.out.println("You lost!");
winnings = 0 - bet;
}
}
return winnings;
}
}
getBet(), but you don't assign that return to anything. And while you are cheating a bit, it would be better forint bet = 0;rather than assigning it to thecharvalue of a space character.