Java 代码的基本循环问题 - 如何允许玩家在终端中多次赌博?

问题描述 投票:0回答:1

我一两周前才开始学习 Java,我想在终端内创建一个愚蠢的小赌博软件;然而,即使我希望他们能够赌博多次,玩家也只能赌博一次。

代码:

import java.util.Random;
import java.util.Scanner;

public class Gambler {

    public static void main(String[] args) {
        Random random = new Random();
        Scanner scanner = new Scanner(System.in);
        int balance = 100;
        System.out.println();
        System.out.println("--> Current Balance: $" + balance + " <--");
        System.out.println();
        System.out.println("Would you like to enter the Java Gambler simulator?");
        System.out.println();
        System.out.println("[Yes] [No]");
        System.out.println();
        String yesOrNo;
        yesOrNo = scanner.nextLine();
        if (yesOrNo.equalsIgnoreCase("Yes")) {
            System.out.println();
            System.out.println("[!] Welcome to the Java Gambler! [!]");
            System.out.println();
            System.out.println("The only Java gambling software in all of Java County!");
            System.out.println();
            System.out.println("If the chosen number is {1}, the player automatically wins $20.");
            System.out.println("If the chosen number is {2}, the player automatically wins $10.");
            System.out.println("If the chosen number is {3}, the player automatically wins $5.");
            System.out.println("All other numbers are an automatic loss.");
            System.out.println();
            System.out.println("Continue?");
            System.out.println();
            System.out.println("[Yes] [No]");
            System.out.println();
            String yesOrNo2;
            yesOrNo2 = scanner.nextLine();
            if (yesOrNo2.equalsIgnoreCase("Yes")) {
                System.out.println();
                System.out.println("Gambling Options:");
                System.out.println();
                System.out.println("[A] $1 Bet (1/20 $20, 1/20 $10, 1/20 $5)");
                System.out.println("[B] $5 Bet (1/10 $20, 1/10 $10, 1/10 $5)");
                System.out.println("[C] $10 Bet (1/5 $20, 1/5 $10, 1/5 $5)");
                System.out.println();
                System.out.println("Gambling Selection: ");
                System.out.println();
                String continueGambling = "Yes";
                while (continueGambling.equalsIgnoreCase("Yes")) {
                    String gamblingSelection;
                    gamblingSelection = scanner.nextLine();
                    String a = "A";
                    String b = "B";
                    String c = "C";
                    if (gamblingSelection.equalsIgnoreCase(a)) {
                        balance = balance - 1;
                        int aSelection = random.nextInt(20) + 1;
                        System.out.println();
                        System.out.println("And the number is... " + aSelection);
                        System.out.println();
                        switch (aSelection) {
                            case 1:
                                System.out.println("Congrats, you have won $20!");
                                balance = balance + 20;
                                System.out.println("--> Your new balance: $" + balance + " <--");
                                System.out.println();
                                System.out.println("Continue?");
                                System.out.println();
                                System.out.println("[Yes] [No]");
                                System.out.println();
                                break;
                            case 2:
                                System.out.println("Congrats, you have won $10!");
                                balance = balance + 10;
                                System.out.println("--> Your new balance: $" + balance + " <--");
                                System.out.println();
                                System.out.println("Continue?");
                                System.out.println();
                                System.out.println("[Yes] [No]");
                                System.out.println();
                                break;
                            case 3:
                                System.out.println("Congrats, you have won $5!");
                                balance = balance + 5;
                                System.out.println("--> Your new balance: $" + balance + " <--");
                                System.out.println();
                                System.out.println("Continue?");
                                System.out.println();
                                System.out.println("[Yes] [No]");
                                System.out.println();
                                break;
                            default:
                                System.out.println("Sorry, no luck this time...");
                                System.out.println("--> Your new balance: $" + balance + " <--");
                                System.out.println();
                                System.out.println("Continue?");
                                System.out.println();
                                System.out.println("[Yes] [No]");
                                System.out.println();
                                break;
                        }
                    }
                    else if (gamblingSelection.equalsIgnoreCase(b)) {
                        balance = balance - 5;
                        int bSelection = random.nextInt(10) + 1;
                        System.out.println();
                        System.out.println("And the number is... " + bSelection);
                        System.out.println();
                        switch (bSelection) {
                            case 1:
                                System.out.println("Congrats, you have won $20!");
                                balance = balance + 20;
                                System.out.println("--> Your new balance: $" + balance + " <--");
                                System.out.println();
                                System.out.println("Continue?");
                                System.out.println();
                                System.out.println("[Yes] [No]");
                                System.out.println();
                                break;
                            case 2:
                                System.out.println("Congrats, you have won $10!");
                                balance = balance + 10;
                                System.out.println("--> Your new balance: $" + balance + " <--");
                                System.out.println();
                                System.out.println("Continue?");
                                System.out.println();
                                System.out.println("[Yes] [No]");
                                System.out.println();
                                break;
                            case 3:
                                System.out.println("You won $5! Its a free spin, I guess...");
                                balance = balance + 5;
                                System.out.println("--> Your new balance: $" + balance + " <--");
                                System.out.println();
                                System.out.println("Continue?");
                                System.out.println();
                                System.out.println("[Yes] [No]");
                                System.out.println();
                                break;
                            default:
                                System.out.println("Sorry, no luck this time...");
                                System.out.println("--> Your new balance: $" + balance + " <--");
                                System.out.println();
                                System.out.println("Continue?");
                                System.out.println();
                                System.out.println("[Yes] [No]");
                                System.out.println();
                                break;
                        }
                    }
                    else if (gamblingSelection.equalsIgnoreCase(c)) {
                        balance = balance - 10;
                        int cSelection = random.nextInt(5) + 1;
                        System.out.println();
                        System.out.println("And the number is... " + cSelection);
                        System.out.println();
                        switch (cSelection) {
                            case 1:
                                System.out.println("Congrats, you have won $20!");
                                balance = balance + 20;
                                System.out.println("--> Your new balance: $" + balance + " <--");
                                System.out.println();
                                System.out.println("Continue?");
                                System.out.println();
                                System.out.println("[Yes] [No]");
                                System.out.println();
                                break;
                            case 2:
                                System.out.println("You won $10! Its a free spin, I guess...");
                                balance = balance + 10;
                                System.out.println("--> Your new balance: $" + balance + " <--");
                                System.out.println();
                                System.out.println("Continue?");
                                System.out.println();
                                System.out.println("[Yes] [No]");
                                System.out.println();
                                break;
                            case 3:
                                System.out.println("You won $5! At least its somethin...");
                                balance = balance + 5;
                                System.out.println("--> Your new balance: $" + balance + " <--");
                                System.out.println();
                                System.out.println("Continue?");
                                System.out.println();
                                System.out.println("[Yes] [No]");
                                System.out.println();
                                break;
                            default:
                                System.out.println("Sorry, no luck this time...");
                                System.out.println("--> Your new balance: $" + balance + " <--");
                                System.out.println();
                                System.out.println("Continue?");
                                System.out.println();
                                System.out.println("[Yes] [No]");
                                System.out.println();
                                break;
                        }
                    }
                }
                continueGambling = scanner.nextLine();
            }
            else {
                String wrongValue = "Invalid response. Please try again.";
                System.out.println(wrongValue);
            }
        }
        else if (yesOrNo.equalsIgnoreCase("No")) {
            System.out.println("You sure are missing out...");
        }
        else {
            System.out.println("Invalid response. Please try again.");
        }
        scanner.close();
    }
}

这里是 GitHub 上完整代码的链接:https://github.com/OkBrock/Gambler

我做了一个循环并制作了一个扫描仪,但每次我输入“是”时什么也没有发生。

总结:
我需要玩家有能力以单一的平衡进行多次赌博。 我用扫描仪做了一个循环,但什么也没发生。检查代码看看。

java loops terminal java.util.scanner
1个回答
0
投票

我为“是”做了这项工作,但是您确实提到您刚刚开始编码,所以我希望您使用调试器来找出当我输入“否”或其他内容时它所面临的问题。 do-while 循环是一个即使底部的测试用例不正确也会肯定运行 1 次的循环。第一次运行后,只有底部的条件为真时才会运行。祝你好运!

import java.util.Random;
import java.util.Scanner;

public class Gambler {

    public static void main(String[] args) {
        Random random = new Random();
        Scanner scanner = new Scanner(System.in);
        int balance = 100;
        System.out.println();
        System.out.println("--> Current Balance: $" + balance + " <--");
        System.out.println();
        System.out.println("Would you like to enter the Java Gambler simulator?");
        System.out.println();
        System.out.println("[Yes] [No]");
        System.out.println();
        String yesOrNo;

        do {
            yesOrNo = scanner.nextLine();
            System.out.println();
            System.out.println("[!] Welcome to the Java Gambler! [!]");
            System.out.println();
            System.out.println("The only Java gambling software in all of Java County!");
            System.out.println();
            System.out.println("If the chosen number is {1}, the player automatically wins $20.");
            System.out.println("If the chosen number is {2}, the player automatically wins $10.");
            System.out.println("If the chosen number is {3}, the player automatically wins $5.");
            System.out.println("All other numbers are an automatic loss.");
            System.out.println();
            System.out.println("Continue?");
            System.out.println();
            System.out.println("[Yes] [No]");
            System.out.println();
            String yesOrNo2;
            yesOrNo2 = scanner.nextLine();
            if (yesOrNo2.equalsIgnoreCase("Yes")) {
                System.out.println();
                System.out.println("Gambling Options:");
                System.out.println();
                System.out.println("[A] $1 Bet (1/20 $20, 1/20 $10, 1/20 $5)");
                System.out.println("[B] $5 Bet (1/10 $20, 1/10 $10, 1/10 $5)");
                System.out.println("[C] $10 Bet (1/5 $20, 1/5 $10, 1/5 $5)");
                System.out.println();
                System.out.println("Gambling Selection: ");
                System.out.println();
                String continueGambling;
                do {
                    String gamblingSelection = scanner.nextLine();
                    String a = "A";
                    String b = "B";
                    String c = "C";
                    if (gamblingSelection.equalsIgnoreCase(a)) {
                        balance -= 1;
                        int aSelection = random.nextInt(20) + 1;
                        System.out.println();
                        System.out.println("And the number is... " + aSelection);
                        System.out.println();
                        switch (aSelection) {
                            case 1:
                                System.out.println("Congrats, you have won $20!");
                                balance += 20;
                                break;
                            case 2:
                                System.out.println("Congrats, you have won $10!");
                                balance += 10;
                                break;
                            case 3:
                                System.out.println("Congrats, you have won $5!");
                                balance += 5;
                                break;
                            default:
                                System.out.println("Sorry, no luck this time...");
                                break;
                        }
                    } else if (gamblingSelection.equalsIgnoreCase(b)) {
                        balance -= 5;
                        int bSelection = random.nextInt(10) + 1;
                        System.out.println();
                        System.out.println("And the number is... " + bSelection);
                        System.out.println();
                        switch (bSelection) {
                            case 1:
                                System.out.println("Congrats, you have won $20!");
                                balance += 20;
                                break;
                            case 2:
                                System.out.println("Congrats, you have won $10!");
                                balance += 10;
                                break;
                            case 3:
                                System.out.println("You won $5! It's a free spin, I guess...");
                                balance += 5;
                                break;
                            default:
                                System.out.println("Sorry, no luck this time...");
                                break;
                        }
                    } else if (gamblingSelection.equalsIgnoreCase(c)) {
                        balance -= 10;
                        int cSelection = random.nextInt(5) + 1;
                        System.out.println();
                        System.out.println("And the number is... " + cSelection);
                        System.out.println();
                        switch (cSelection) {
                            case 1:
                                System.out.println("Congrats, you have won $20!");
                                balance += 20;
                                break;
                            case 2:
                                System.out.println("You won $10! It's a free spin, I guess...");
                                balance += 10;
                                break;
                            case 3:
                                System.out.println("You won $5! At least it's something...");
                                balance += 5;
                                break;
                            default:
                                System.out.println("Sorry, no luck this time...");
                                break;
                        }
                    }
                    System.out.println("--> Your new balance: $" + balance + " <--");
                    System.out.println();
                    System.out.println("Continue?");
                    System.out.println("[Yes] [No]");
                    System.out.println();
                    continueGambling = scanner.nextLine();
                } while (continueGambling.equalsIgnoreCase("Yes")); 
            } else if (yesOrNo.equalsIgnoreCase("No")) {
                System.out.println("You sure are missing out...");
            } else {
                System.out.println("Invalid response. Please try again.");
            }
        } while (yesOrNo.equalsIgnoreCase("Yes"));
        scanner.close();
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.