Java老虎机循环

Java老虎机循环,java,Java,我对Java还是相当陌生,所以我在编写这个老虎机程序时遇到了一些问题。在你第一次运行程序并计算奖励后,比如说,你下注20美元,赢了40美元,那么你的新金额是120美元,它应该循环并提示你想下注多少?再次,然后用你的新金额完成游戏,这样你就可以赌120美元的一部分,而不是游戏开始时给你的100美元,然后继续这样做,直到你的钱用完为止。但现在它的工作方式是,游戏继续完全重新开始,所以用户循环,每次都从100美元开始。有什么建议吗 import java.util.Random; import jav

我对Java还是相当陌生,所以我在编写这个老虎机程序时遇到了一些问题。在你第一次运行程序并计算奖励后,比如说,你下注20美元,赢了40美元,那么你的新金额是120美元,它应该循环并提示你想下注多少?再次,然后用你的新金额完成游戏,这样你就可以赌120美元的一部分,而不是游戏开始时给你的100美元,然后继续这样做,直到你的钱用完为止。但现在它的工作方式是,游戏继续完全重新开始,所以用户循环,每次都从100美元开始。有什么建议吗

import java.util.Random;
import java.util.Scanner; 
import java.io.*;
public class SlotMachineSimulation
{
    public static void main(String [] args)
    {
        Scanner user_input = new Scanner (System.in);
        Random randInt = new Random();

        System.out.print("Welcome to the Slot Machine Simulator!\n" + "\nActions:" + "\n1. Start a new game" + "\n2. View scores" + "\n3. Exit\n");
        System.out.print("\nPlease select an action: ");
        int userAction = user_input.nextInt();

        if (userAction == 1)
        {
            System.out.print("\nBefore the game begins, please enter your name: ");
            String userName = user_input.next();
            System.out.print("\nGame start! You will begin with $100.00." + "\nEnter a negative value to quit the game. " + "Good luck, " + userName + "!\n");

            double startingSum = 100.00;
            double userTotal = 0.0;
            System.out.printf("\nYou currently have: $%.2f\n", startingSum);

            do 
            {
                System.out.print("\nHow much would you like to bet? ");
                double userBet1 = user_input.nextDouble();  
                userTotal = (startingSum - userBet1);

                //Beginning of random number generation 
                int val1 = randInt.nextInt(6) + 1;
                int val2 = randInt.nextInt(6) + 1;
                int val3 = randInt.nextInt(6) + 1;
                String valName1 = " ", valName2 = " ", valName3 = " ";  //For assignment of slot machine names to randomly generated values

                switch (val1)
                {
                    case 1:
                        valName1 = "Cherries";
                        break;
                    case 2:
                        valName1 = "Oranges";
                        break;
                    case 3:
                        valName1 = "Plums";
                        break;
                    case 4:
                        valName1 = "Bells";
                        break;
                    case 5:
                        valName1 = "Melons";
                        break;
                    case 6:
                        valName1 = "Bars";
                        break;
                }

                switch (val2)
                {
                    case 1:
                        valName2 = "Cherries";
                        break;
                    case 2:
                        valName2 = "Oranges";
                        break;
                    case 3:
                        valName2 = "Plums";
                        break;
                    case 4:
                        valName2 = "Bells";
                        break;
                    case 5:
                        valName2 = "Melons";
                        break;
                    case 6:
                        valName2 = "Bars";
                        break;
                }

                switch (val3)
                {
                    case 1:
                        valName3 = "Cherries";
                        break;
                    case 2:
                        valName3 = "Oranges";
                        break;
                    case 3:
                        valName3 = "Plums";
                        break;
                    case 4:
                        valName3 = "Bells";
                        break;
                    case 5:
                        valName3 = "Melons";
                        break;
                    case 6:
                        valName3 = "Bars";
                        break;
                }

                System.out.println("\n-------------------------------");
                System.out.printf("%-12s%-10s%5s\n", valName1, valName2, valName3);
                System.out.print("-------------------------------\n");

                //Beginning of reward calculation
                if (val1 == val2 || val2 == val3 || val1 == val3)
                {
                    System.out.println("\nNumber of matches: 1");
                    double doubleReward = (userBet1 * 2);
                    double postBetSum = (userTotal + doubleReward);
                    System.out.printf("You have won: $%.2f", doubleReward);
                    System.out.printf("\nYou currently have: $%.2f", postBetSum);
                }
                else if (val1 == val2 && val2 == val3)
                {
                    System.out.println("\nNumber of matches: 3");
                    double tripleReward = (userBet1 * 3);
                    double postBetSum = (userTotal + tripleReward);
                    System.out.printf("\nYou have won: $%.2f",tripleReward);
                    System.out.printf("\nYou currently have: $%.2f", postBetSum);
                }
                else
                {
                    System.out.println("\nNumber of matches: 0");
                    System.out.println("You have won: $0.00");
                    System.out.printf("You currently have: $%.2f", userTotal);
                }
            } while (userTotal > 0.00);
        } //end of #1 option
    }
}

很简单,在循环开始时,您将总计分配为:

userTotal = (startingSum - userBet1);
这意味着

userTotal = ($100 - userBet1);
因此,很明显,用户在每一轮之后都会重置为100美元

请尝试以下方法:

double userTotal = startingSum;
...
userTotal -= userBet1;

还有一些其他的问题,如三个相同的case语句体,应该用一个循环缩成一个,并且诚实地考虑把程序放在上面是值得的。由于您可以获得的反馈将为您提供更多的学习Java和防止错误的方法,例如本程序中的错误。

更改这一行,应该会奏效

double userTotal = 100.0;
userTotal -= userBet1;
使用偶数

String valName1 = genName(), valName2 = genName(), valName3 = genName();
方法

public static String genName()
{
    int ran = new Random().nextInt(6) + 1;
    switch (ran)
    {
        case 1:
            return "Cherries";
        case 2:
            return "Oranges";
        case 3:
            return "Plums";
        case 4:
            return "Bells";
        case 5:
            return "Melons";
        case 6:
            return "Bars";
            default:
                return "Cherries";
    }
}
要清除代码中的代码问题,请执行以下操作:

对于循环的每个迭代,userTotal是100.00-userBet1。通过这种方式编写代码,您不会为下一次迭代维护userTotal值。 在if-else-if梯形图中,如果第一个条件匹配,则控制退出if-else-if构造,用户将永远不会获得三重奖励。 在if-else-if块中,您没有维护userTotal。您只需将其分配给局部变量postBetSum。 解决方案:

在循环外声明并初始化count=0变量。内部循环只需将计数器增加1。检查它是否是第一次迭代,即count==1,然后userTotal=100.0-userBet1。否则userTotal=userTotal-userBet1 在if-else-if梯形图中首先设置第二个条件。通过这种方式,您可以确定,根据您的代码匹配,如果有三个或两个,那么您将获得三重奖励。如果没有,那么它将检查下一个else-if块是否匹配,如果找到,那么用户将获得双倍奖励。 您可以通过以下方式维护userTotal userTotal+=doubleReward。此语句确保userTotal在获得奖励后保留其值。 所有这些,加上你可以把你的开关构造放在一个只返回一个字符串的方法中,而不是写三次

现在试试这个:

class SlotMachineSimulation
{
 public static String getValName(int val){
    String valName=" ";
    switch (val)
    {
        case 1:
            valName = "Cherries";
            break;
        case 2:
            valName = "Oranges";
            break;
        case 3:
            valName = "Plums";
            break;
        case 4:
            valName = "Bells";
            break;
        case 5:
            valName = "Melons";
            break;
        case 6:
            valName = "Bars";
            break;
    }
    return valName;

}

public static void main(String [] args)
{
    Scanner user_input = new Scanner (System.in);
    Random randInt = new Random();

    System.out.print("Welcome to the Slot Machine Simulator!\n" + "\nActions:" + "\n1. Start a new game" + "\n2. View scores" + "\n3. Exit\n");
    System.out.print("\nPlease select an action: ");
    int userAction = user_input.nextInt();

    if (userAction == 1)
    {
        System.out.print("\nBefore the game begins, please enter your name: ");
        String userName = user_input.next();
        System.out.print("\nGame start! You will begin with $100.00." + "\nEnter a negative value to quit the game. " + "Good luck, " + userName + "!\n");


        double userTotal = 0.0;
        System.out.printf("\nYou currently have: $%.2f\n", 100.00);
        int count=0;
        do 
        {
            System.out.print("\nHow much would you like to bet? ");
            double userBet1 = user_input.nextDouble();
            count++;
            if(count==1){
                userTotal=100.0-userBet1;
            }else{
                userTotal=userTotal-userBet1;
            }


            //Beginning of random number generation 
            int val1 = randInt.nextInt(6) + 1;
            int val2 = randInt.nextInt(6) + 1;
            int val3 = randInt.nextInt(6) + 1;

            String valName1,valName2,valName3;  //For assignment of slot machine names to randomly generated values

            valName1=getValName(val1);
            valName2=getValName(val2);
            valName3=getValName(val3);

            System.out.println("\n-------------------------------");
            System.out.printf("%-12s%-10s%5s\n", valName1, valName2, valName3);
            System.out.print("-------------------------------\n");

            //Beginning of reward calculation
            if (val1 == val2 && val2 == val3 && val3==val1)//check this condition first otherwise even after matching these values user never get more than double
            {
                System.out.println("\nNumber of matches: 1");
                double doubleReward = (userBet1 * 2);
                userTotal+=doubleReward;

                System.out.printf("You have won: $%.2f", doubleReward);
                System.out.printf("\nYou currently have: $%.2f", userTotal);
            }
            else if (val1 == val2 || val2 == val3 || val1 == val3)
            {
                System.out.println("\nNumber of matches: 3");
                double tripleReward = (userBet1 * 3);
                userTotal+=tripleReward;

                System.out.printf("\nYou have won: $%.2f",tripleReward);
                System.out.printf("\nYou currently have: $%.2f", userTotal);
            }
            else
            {
                System.out.println("\nNumber of matches: 0");
                System.out.println("You have won: $0.00");
                System.out.printf("You currently have: $%.2f", userTotal);
            }
        } while (userTotal > 0.00);
    } //end of #1 option
    user_input.close();
 }
}

我测试了这段代码,这个问题没有发生在我身上。这就是你的代码吗?我看到一个可能的错误,我有32个,我玩31个,我输了,他给我69$…userTotal=startingSum-userBet1;可能应该是userTotal-=userBet1;,userTotal可能应该初始化为100。你的老虎机永远不会出三倍的钱。任何三元组也是双元组,您首先要检查双元组。您可能希望将字符串与.equals进行比较,而不是==。您的答案很好-除了提到代码检查的部分。把那部分去掉。请。根据主题:虽然类似的问题可能在这里的主题,这些问题往往以一种不太可能帮助未来读者的方式解决。我的。对于专业和狂热的程序员stackoverflow的观众来说,这个问题中的错误是非常明显的,因此在我看来,这个问题对社区来说真的没有任何价值。我认为它在codereview.stackexchange.com上有价值,所以我不会删除我答案中的这一部分。因为这个问题是关于故障排除、调试或理解代码片段的,所以它在代码审查上肯定没有价值。它几乎不是一个代码片段,而是一个完整的程序。无论如何,我要澄清的是,独立于AsKER的调试问题,他应该考虑把他的代码带到CODEDEVIEW,因为代码中有几个问题可以在那里解决。您的答案足够简洁,以满足主要问题,但额外的位有点……很多。你的回答是对的,但是这里有很多额外的噪音。你是在骗我吗?其他人甚至写了我写的代码和问题的代码,你说这里有很多额外的噪音?都是代码,没有解释。我更愿意看到关于代码为何工作的解释。