簡體   English   中英

如何在Java程序中基於用戶輸入繼續循環

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

我有一個彩票程序,可以運行投注並隨機生成數字。 當用戶的總獎金達到$ 5000時,該用戶可以選擇兌現$ 5000並使用剩余余額繼續循環。 如果選擇不套現,用戶也將能夠結束循環。 如何根據用戶輸入繼續循環? 如果用戶輸入是,則循環將繼續,但是我不確定如何執行此操作。 有人可以幫忙嗎? 如果應該繼續循環,則由於兌現而從用戶的總勝利中刪除了$ 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);

            }

所以在我的代碼中,我有循環

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

此循環應接收用戶輸入,然后從總贏額中減去5000,並使用剩余的預算繼續玩游戲。 不知道為什么如果我有-5000,它會增加。

(如果可能,請幫助我將答案更改為是/否,而不是輸入1)

我假設您正在使用java.util.Scanner 這是答案;

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çinAkçay解釋了如何將輸入從int更改為String。

我看了看您的代碼,並使用它制作了一個可運行的main方法,它可以按您期望的那樣工作。 可能需要從初始預算中扣除5k的事實令人困惑嗎?

if ((budget - initBudget) >= 5000)

這是我用來檢查代碼的整個類,是否有幫助:

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

        }
    }
}

您可以這樣更改代碼:

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)

在這里,我使用了String (將用戶的響應存儲為單詞)。 如果他輸入YesyesYES (忽略大小寫,則認為用戶要兌現),循環不會中斷。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM