I was expecting the do-while loop to end every time I typed in 'n', but it only happens every other time. I have not noticed any patterns within this. I also try to have the loop finish either if the number of points is 0 or less, but this does not seem to work either.
I have been trying this many times, but cannot see where the fault is. Just require some assistance please and thanks.
Here is the problem:
Here is my code:
package highlow;
import java.util.Scanner;
public class Highlow {
public static void Rules() {
System.out.println("RULES:");
System.out.println("Numbers 1-6 are low ");
System.out.println("Numbers 8-13");
System.out.println("Number 7 is neither high or low\n\n");
}
public static void main(String[] args) {
int number;
int gambleAmount;
int prediction;
int correctRange;
int winnings;
int points;
int numberOfGuesses = 0;
String repeat;
Scanner input = new Scanner(System.in);
points = 1000;
System.out.print("High Low Game\n\n");
Rules();
do {
number = (int) (13 * Math.random() + 1);
if (number > 7) {
correctRange = 1;
}
else if (number < 7) {
correctRange = 0;
}
else {
correctRange = -1;
}
numberOfGuesses += 1;
System.out.println("You have " + points + " points");
do {
System.out.println("Please insert a valid number of points");
System.out.print("How many points would you like to gamble: ");
gambleAmount = input.nextInt();
} while (gambleAmount > points);
System.out.print("Predict [1=high, 0=low]: ");
prediction = input.nextInt();
if (prediction == correctRange) {
winnings = gambleAmount * 2;
points = points + winnings;
System.out.println("The number was " + number);
System.out.print("You win!\n\n");
}
else {
points = points - gambleAmount;
System.out.println("The number was " + number);
System.out.print("You lose\n\n");
}
System.out.print("Would you like to play again ('n' for no and 'y' for yes): ");
repeat = input.next();
} while (repeat.equals("n") || points != 0);
System.out.print("You had " + numberOfGuesses + " guesses");
}
}