Java 方法未传递到 main

标签 java

所以,我似乎在通过所有三种方法时遇到问题

机器播放一个

机器玩两个

机器玩三

进入主函数,以便那些 if 语句激活并开始计数/工作。我知道为了打印我需要使用的方法

System.out.println(displayMachineOne());

但我只是想让这些 if 语句在 main 中工作,以便让主计数器工作。

如果需要上下文:目标是计算 vicie 按 1-2-3-1-2-3-1 等顺序玩可预测的老虎机需要多长时间才能破产。

现在它只运行循环 100 次(因为有 100 个硬币),然后她就破产了,从未赢得过任何东西。

我也很确定我也需要返回总季度数,但首先我想尝试让方法正确通过。

感谢任何帮助。 (是的,我确实尝试过谷歌,但我似乎找不到我要找的东西)

public class WinningBig {

    public static void main(String[] args) {
        int totalQuarters = 100;
        int totalPlays = 0;
        int machineOnePlays = 0;
        int machineTwoPlays = 0;
        int machineThreePlays = 0;

        while(true)
        {
            if(totalQuarters > 0) {
                totalQuarters = displayMachineOne(totalQuarters, machineOnePlays);
                machineOnePlays ++;
                totalPlays ++;
                totalQuarters--;

            }
            if(totalQuarters > 0) {
            totalQuarters = displayMachineTwo(totalQuarters, machineTwoPlays);
            machineTwoPlays++;
            totalPlays++;
            totalQuarters--;
            }
            if(totalQuarters > 0) {
                totalQuarters = displayMachineThree(totalQuarters, machineThreePlays);
                machineThreePlays++;
                totalPlays++;
                totalQuarters--;
                }else {
            System.out.println("Vickie lost all of her money! it took    " +
                    totalPlays + " plays for her to go broke");
            return;
        }
        }
    }

    public static int displayMachineOne(int totalQuarters,
                                           int machineOnePlays) {
         machineOnePlays++;

        if(machineOnePlays == 35) {

            totalQuarters += 25;
            machineOnePlays = 0;

            System.out.println("Vickie won on Machine One in the amount of 25 quarters, her total is now " + (totalQuarters) * .25);

        }
        return totalQuarters;

    }

    public static int displayMachineTwo(int totalQuarters,
                                           int machineTwoPlays) {
            machineTwoPlays++;

        if(machineTwoPlays == 100) {

            totalQuarters += 75;
            machineTwoPlays = 0;

            System.out.println("Vickie won on Machine One in the amount of 75 quarters, her total is now " + (totalQuarters) * .25);

        }
        return totalQuarters;
    }
    public static int displayMachineThree(int
            totalQuarters,int machineThreePlays) {

            machineThreePlays++;
        if(machineThreePlays == 8) {
            totalQuarters += 5;
            machineThreePlays = 0;

            System.out.println("Vickie won on Machine One in the amount of 5 quarters, her total is now " + (totalQuarters) * .25);

        }
        return totalQuarters;

    }
}

最佳答案

public class WinningBig {
    static int totalQuarters = 100;
    static int totalPlays = 0;
    static int machineOnePlays = 0;
    static int machineTwoPlays = 0;
    static int machineThreePlays = 0;

    public static void main(String[] args) {
        while (true) {
            if (totalQuarters > 0) {
                displayMachineOne();
                machineOnePlays++;
                totalPlays++;
                totalQuarters--;

            }
            if (totalQuarters > 0) {
                displayMachineTwo();
                machineTwoPlays++;
                totalPlays++;
                totalQuarters--;
            }
            if (totalQuarters > 0) {
                displayMachineThree();
                machineThreePlays++;
                totalPlays++;
                totalQuarters--;
            } else {
                System.out.println("Vickie lost all of her money! it took    " +
                        totalPlays + " plays for her to go broke");
                return;
            }
        }
    }

    public static void displayMachineOne() {

        if (machineOnePlays == 35) {

            totalQuarters += 25;
            machineOnePlays = 0;

            System.out.println("Vickie won on Machine One in the amount of 25 quarters, her total is now " + (totalQuarters - 1) * .25);

        }

    }

    public static void displayMachineTwo() {


        if (machineTwoPlays == 100) {

            totalQuarters += 75;
            machineTwoPlays = 0;

            System.out.println("Vickie won on Machine One in the amount of 75 quarters, her total is now " + (totalQuarters - 1) * .25);

        }
    }

    public static void displayMachineThree() {


        if (machineThreePlays == 8) {
            totalQuarters += 5;
            machineThreePlays = 0;

            System.out.println("Vickie won on Machine One in the amount of 5 quarters, her total is now " + (totalQuarters - 1) * .25);

        }
    }
}

关于Java 方法未传递到 main,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52416894/

相关文章:

java - Android:设置 utf-8 编码为字符串和数组

java - Gradle + hibernate

java - 在 Heroku 上部署 Java + Groovy 混合代码时出错

java - View的android :onClick as static method,可能吗?

java - 在 Netbeans 中显示文件夹中的随机图像到 JLabel

java - 将子类转换为父类(super class)后调用方法的有趣行为

java - Spring批处理spring jpa错误CannotCreateTransactionException : Could not open JPA EntityManager for transaction IllegalStateException:

Java SWT 表列删除

java - JPA Hibernate 和具有用于 map 的 ElementCollection

java - 在 JTabbedPane 上调整 JScrollPane 的大小