为什么在for循环中的此变量未重置为初始值?

时间:2019-04-12 14:27:37

标签: java for-loop variables while-loop variable-assignment

我有这个程序代码,试图创建类似于蒙特卡洛模拟的程序。从本质上讲,赌徒先下注给定的£赌注,然后下注£1的合理赌注,直到破产或达到目标为止。

程序代码按预期方式工作,但是我遇到的问题是语句“ int Cash =股份;”我想知道如何在每次迭代中正确更新变量“ cash”。

我对为什么不将其重置回“ stake”的初始值感到困惑,这是用户在main for循环之外输入的。例如,如果目标是500,而本金是100,为什么现金不总是保持100,如果是,那么它将如何达到目标500?

public static void monteCarlo() {
    Scanner input = new Scanner(System.in);
    System.out.println("Please enter stake, goal, " + " and number of tries");
    int stake = input.nextInt();
    int goal = input.nextInt();
    int tries = input.nextInt();
    int wins = 0;
    // repeat experiment
    for (int i = 0; i < tries; i++) {
        // do one gambler's ruin experiment
        int cash = stake;
        while (cash > 0 && cash < goal) {
            // flip coin and update
            if (Math.random() < 0.5) cash++;
            else cash--;
        }

        if (cash >= goal) {
            wins++;
        }
    }

    System.out.println(wins + " wins of " + tries);
}

2 个答案:

答案 0 :(得分:0)

 if (Math.random() < 0.5) cash++;

cash在这里有50%的机会递增。平均而言,cash的值保持不变。

要使cash从100增至500,您需要在游戏中以50%的获胜几率赢得400

继续运行该程序,您可能会很幸运:)

答案 1 :(得分:0)

int cash = stake 执行(将现金变量设置为始终相同的初始值!)

如果您输入5个试验和12个赌注,则

  1. 现金重置为12。
  2. while循环在每次试用时都会增加/减少现金
  3. 直到达到目标或没有更多现金

从1.到3.的步骤将被再次调用5次。每5次都会从12开始,但是cash++cash--指令会对其进行更改,直到达到最终条件为止。

运行下面的详细变体可能会帮助您更清楚地了解它...

package so20190412;

import java.util.Scanner;

public class MC {

    public static void main(String[] args) {
        try(Scanner input = new Scanner(System.in);) {
        System.out.println("Please enter stake, goal, " + " and number of tries");
        int stake = input.nextInt();
        int goal = input.nextInt();
        int tries = input.nextInt();

        monteCarlo(stake, goal, tries);

        }
    }



    public static void monteCarlo(final int stake, final int goal, final int tries) {

        System.out.println("Starting with a stake of " + stake);
        System.out.println("Goal is " + goal);
        System.out.println("Will try : " + tries + " times");

        int wins = 0;
        // repeat experiment
        for (int i = 0; i < tries; i++) {
            // do one gambler's ruin experiment

            System.out.println("### ITERATION " + (i+1));
            int cash = stake;

            System.out.println(cash);
            while (cash > 0 && cash < goal) {
                // flip coin and update
                if (Math.random() < 0.5) {
                    cash++;
                    System.out.println("WIN! get one more € - new cash is " + cash);
                }
                else {
                    cash--;
                System.out.println("LOST one € - new cash is " + cash);
            }
            }

            if (cash >= goal) {
                wins++;
                System.out.println("Cash is over goal - add a win!");
            }
            System.out.println("END OF ITERATION - stake : " + stake + " cash:" + cash + " wins:" + wins);
        }

        System.out.println(wins + " wins of " + tries);
    }

}

希望这可以澄清情况!