1

I have a lottery program that will run bets and randomly generate numbers. When the user reaches $5000 in total wins, that user will have an option to cash out $5000 and use the remaining balance to continue the loop. The user will also be able to end the loop if choosing to not cash out. How do I continue a loop based on user input? If the user writes yes, the loop will continue, but I am not sure how to do that. Can someone please help? If the loop is supposed to continue, $5000 removed from the user's total win because of cashout, the remaining balance will be used to play.

I have tried putting the if statement for user input in multiple places in the main loop, but the user budget keeps increasing, it does not subtract $5000.

java.util.Scanner
while (budget > 0) {

                lottery = (int) (Math.random() * 100);
                guess = (int) (Math.random() * 100);

                budget -= 1;    // budget = budget-1;
                // Get digits from lottery
                int lotteryDigit1 = lottery / 10;
                int lotteryDigit2 = lottery % 10;

                // Get digits from guess
                int guessDigit1 = guess / 10;
                int guessDigit2 = guess % 10;

                System.out.println("The lottery number is " + lottery);
                System.out.println("The computer picked number is " + guess);

                // Check the guess
                if (guess == lottery) {
                    System.out.println("Exact match: you win $10,000");
                    totalWin += 1000;
                } else if (guessDigit2 == lotteryDigit1 && guessDigit1 == lotteryDigit2) {
                    System.out.println("Match all digits: you win $3,000");
                    totalWin += 300;
                } else if (guessDigit1 == lotteryDigit1 || guessDigit1 == lotteryDigit2 || guessDigit2 == lotteryDigit1
                        || guessDigit2 == lotteryDigit2) {

                    System.out.println("Match one digit: you win $1,000");
                    totalWin += 100;
                } else
                    System.out.println("Sorry, no match");

                budget += totalWin;
                totalWin=0;

                if (budget > initBudget)
                    if ((budget-initBudget) >= 5000) {

                        System.out.println("You have reached the goal of 5000, we can go home!");
                        System.out.println("Enter 1 if you would like to cash out and use remaining balance to play and 0 if you would like to stop");
                        int decison = input.nextInt();
                        if (decison==1){budget=budget-5000;
                        System.out.println("Budget After play " + budget);
                        continue;
                        }break;


                    } 

                System.out.println("Budget After play " + budget);

            }

So in my code, I have the loop

System.out.println("Enter 1 if you would like to cash out and use remaining balance to play and 0 if you would like to stop");
int decison = input.nextInt();
    if (decison==1){budget=budget-5000;
System.out.println("Budget After play " + budget);
continue;
}break;
} 

This loop should receive user input and then subtract 5000 from the total win and use the remaining budget to continue play.. but it is only increasing. Don't understand why it is increasing if I have -5000.

(if possible, please help me change the answer to a yes/no rather than input 1)

1
  • Just a side note: be careful with your data and the units; especially when dealing with the users money :p Within the lottery-run your totalWin is increased like this: exact match => "10,000 win" => 1,000 increase. Thats one order of magnitude difference. So when you check the buget later on and it has reached 5,000 the user has actually won 50,000. So either the log-messages or the totalWin values or the condition are wrong. Be careful, this error was copied to the answers as well. Commented May 3, 2019 at 6:24

3 Answers 3

1

I assumed you are using java.util.Scanner. Here is the answer;

System.out.println("Enter yes if you would like to cash out and use remaining balance to play and no if you " +
                "would like to stop");
String decison = input.nextLine();
if ("yes".equals(decison))
{
    budget = budget - 5000;
    System.out.println("Budget After play " + budget);
    continue;
}
break; // break the statement.
Sign up to request clarification or add additional context in comments.

3 Comments

how do I break the loop if user replies no? Also, why do you add a + to the Prinln statement? Thank you for the help!
@chickenwings123 - added break statement too.
@ErçinAkçay Here if the user enters Yes or YES, the loop will still break. Because you are only comparing it with lowercase yes.
1

Erçin Akçay explained how to change the input from an int to a String.

I took a look at your code and made a runnable main method using it, and it works as you would expect. Perhaps the fact that it needs to be 5k from the intial budget is confusing?

if ((budget - initBudget) >= 5000)

Here is the entire class I used to check your code if it is helpful:

import java.util.Scanner;

public class Test {
    private static int budget;
    private static int lottery;
    private static int guess;
    private static int totalWin;
    private static int initBudget = 4500;

    public static void main(String[] args) throws InterruptedException {
        Scanner input = new Scanner(System.in);
        budget = initBudget;

        while (budget > 0) {

            lottery = (int) (Math.random() * 100);
            guess = (int) (Math.random() * 100);

            budget -= 1;    // budget = budget-1;
            // Get digits from lottery
            int lotteryDigit1 = lottery / 10;
            int lotteryDigit2 = lottery % 10;

            // Get digits from guess
            int guessDigit1 = guess / 10;
            int guessDigit2 = guess % 10;

            System.out.println("The lottery number is " + lottery);
            System.out.println("The computer picked number is " + guess);

            // Check the guess
            if (guess == lottery) {
                System.out.println("Exact match: you win $10,000");
                totalWin += 1000;
            } else if (guessDigit2 == lotteryDigit1 && guessDigit1 == lotteryDigit2) {
                System.out.println("Match all digits: you win $3,000");
                totalWin += 300;
            } else if (guessDigit1 == lotteryDigit1 || guessDigit1 == lotteryDigit2 || guessDigit2 == lotteryDigit1
                    || guessDigit2 == lotteryDigit2) {

                System.out.println("Match one digit: you win $1,000");
                totalWin += 100;
            } else {
                System.out.println("Sorry, no match");
            }

            budget += totalWin;
            totalWin = 0;

            if (budget > initBudget) {
                if ((budget - initBudget) >= 5000) {

                    System.out.println("You have reached the goal of 5000, we can go home!");
                    System.out.println(
                            "Enter 1 if you would like to cash out and use remaining balance to play and 0 if you "
                                    + "would like to stop");
                    int decison = input.nextInt();
                    if (decison == 1) {
                        budget = budget - 5000;
                        System.out.println("Budget After play " + budget);
                        continue;
                    }
                    break;


                }
            }

            System.out.println("Budget After play " + budget);

        }
    }
}

3 Comments

Thank you for the time you spent trying to help me! I tried to run your code, but it would not run? Why?
I'm not sure, what output did you get when you tried to run it?
I don't have a high enough reputation to comment on the other answer, but if you have the same break after if ("yes".equals(decison)) { budget = budget - 5000; System.out.println("Budget After play " + budget); continue; } then it will behave the same way. i.e. anything other than yes will terminate the program.
0

You can change your code like this:

System.out.println("Enter yes if you would like to cash out and use remaining balance to play and no if you would like to stop");
String decision = input.next();
if ("Yes".equalsIgnoreCase(decision)) { // Cashing Out
    budget = budget - 5000;
    System.out.println("Budget After play " + budget);
    continue;
}
break; // breaking the loop (if user enters anything other than yes)

Here I have used String (to store User's response as a word). If he enters Yes, yes or YES (ignoring the case, it will be considered that user wants to cash out) the loop will not break.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.