简体   繁体   English

如何在Java程序中基于用户输入继续循环

[英]How to continue loop based on user input in java program

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. 当用户的总奖金达到$ 5000时,该用户可以选择兑现$ 5000并使用剩余余额继续循环。 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. 如果应该继续循环,则由于兑现而从用户的总胜利中删除了$ 5000,剩余的余额将用于游戏。

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. 我尝试将if语句供用户输入放在主循环中的多个位置,但用户预算一直在增加,它不会减去$ 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. 此循环应接收用户输入,然后从总赢额中减去5000,并使用剩余的预算继续玩游戏。 Don't understand why it is increasing if I have -5000. 不知道为什么如果我有-5000,它会增加。

(if possible, please help me change the answer to a yes/no rather than input 1) (如果可能,请帮助我将答案更改为是/否,而不是输入1)

I assumed you are using java.util.Scanner . 我假设您正在使用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.

Erçin Akçay explained how to change the input from an int to a String. ErçinAkçay解释了如何将输入从int更改为String。

I took a look at your code and made a runnable main method using it, and it works as you would expect. 我看了看您的代码,并使用它制作了一个可运行的main方法,它可以按您期望的那样工作。 Perhaps the fact that it needs to be 5k from the intial budget is confusing? 可能需要从初始预算中扣除5k的事实令人困惑吗?

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);

        }
    }
}

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). 在这里,我使用了String (将用户的响应存储为单词)。 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. 如果他输入YesyesYES (忽略大小写,则认为用户要兑现),循环不会中断。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM