0

Alright, I am making a HiLo game and I found an issue with the code. The player starts with 1000 points but they can place a bet higher than the number of points they have. I want it so they can not bet more than what they have

int playerPoints = 1000;
int betPoints;
int predict;
int randomNumber;

System.out.println("Enter the points you want to risk");
betPoints = input.nextInt();

I need betPoints to be less than or equal to playerPoints. How can I restrict the integer to obey that?

2
  • 1
    What should happen if they enter a number that's too high? Commented May 17, 2019 at 18:08
  • 1
    Place the input collection into a loop (like a while loop), add an if statement to check for validity, and then display an error and recollect if necessary. BTW, you also need to ensure they do not enter, presumably <=0. Commented May 17, 2019 at 18:09

2 Answers 2

2

With Math.min() for example :

betPoints = Math.min(playerPoints, betPoints);
Sign up to request clarification or add additional context in comments.

Comments

0

change betPoints = input.nextInt(); with,

do {
    betPoints = input.nextInt();
} while(betPoints > playerPoints);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.