Program skipping 'if' Statement and defaults to 'else'
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;
}
}
2 answers
-
answered 2018-10-11 19:20
Dawood ibn Kareem
The part you haven't understood is that every method - in fact, every call to every method - has its own collection of local variables. That means that
- the
winnings
variable declared inmain
is NOT the same variable as thewinnings
variable declared indetermineWinnings
; - the
bet
variable declared inmain
is NOT the same variable as thebet
variable declared ingetBet
.
What you need to do is to make sure that the value returned by each called method is assigned to the variable that you want to store it in, in the caller method. So in
main
, when you callgetBet
, you actually want to writebet = getBet(inScanner, currentPool);
so that the value returned from
getBet
gets assigned to thebet
variable frommain
. Similarly, when you calldetermineWinnings
, you need to writewinnings = determineWinnings(highLow, bet, roll);
so that the value returned from
determineWinnings
gets assigned to thewinnings
variable frommain
.If you don't do that, then all the variables in
main
just keep their original values, which are100
forcurrentPool
and32
for winnings (because' '
is just another way to write32
). That's why your final value turns out to be132
. - the
-
answered 2018-10-11 19:32
sachin patel
To avoid skipping if in execution use
bet = getBet(inScanner, currentPool); highLow = getHighLow(inScanner); winnings = determineWinnings(highLow, bet, roll);
instead of directly calling
getBet(inScanner, currentPool); getHighLow(inScanner); determineWinnings(highLow, bet, roll);
Reson for skipping if statement and wrong answer:
getBet(inScanner, currentPool) method is returning the value which is not assigned to
int bet = ' '; java tries to convert ' ' to an integer which is 32. so bet is every time assigned 32 value when you call getBet(inScanner, currentPool) even if the entered value is different(which will give wrong answer).
In the case of getHighLow(inScanner) method is returning char value but since it is not assigned to highLow, highLow will always have value ' '.
since highLow has not assigned the actual value(H/L/S), if statement will be skipped as ' '!=(H/L/S) and always statements in else are executed.