6
|  * |
| *  |
|  * |
| *  |
|  * |
| *  |
|  * |
|  * |
| *  |
| *  |
|  * |
| *  |
|*   |

我需要为赌徒的废墟问题打印上面的 ASCII 图。将赌注和目标作为参数。最左边| 代表0美元,最右边| 代表目标,* 代表手头现金。我的程序如下:

  public class RuinPath {

  public static void main(String[] args) {
        // TODO - Your solution

    int stake = Integer.parseInt(args[0]);    // gambler's stating bankroll
    int goal = Integer.parseInt(args[1]);    // gambler's desired bankroll

    {
      int i = 0;
      if (i == 0) {
        System.out.print("|");
        i++;
        while (i < stake) {
          System.out.print(" ");
          i++;

          if (i == stake) {
            System.out.print("*");
            i++;

            while (i > stake && i < goal) {
              System.out.print(" ");
              i++;

              if (i == goal) {
                System.out.print("|");
                i = 0;
                System.out.println();

                {

                  if (Math.random() < 0.5) {

                    stake++;     // win $1
                  } else {
                    stake--;    // lose $1

                  }

                  if (stake == 1 || stake == goal - 1);
                  break;

                }
              }
            }
          }
        }
      }
    }
  }
}

该程序打印的内容是:

|    *    |
    *     |
     *    |
      *   |
       *  |
      *   |
       *  |
      *   |
       *  |
      *   |
       *  |
      *   |
       *  |
        * |
         *

为什么我的程序不循环,这样我就可以得到最左边 | 似乎一直代表 0 美元?我有它所以 i = 0; 在循环结束时,当它返回时它应该重新循环,直到赌注为 1 或小于目标。相反,它从程序中间重新循环。

4

2 回答 2

1

你的逻辑有点太复杂了。此外,您的缩进将使任何类型的调试都变得极其困难。

一步一步来:

public class RuinPath {
    public static void main(String[] args) {
        int stake = Integer.parseInt(args[0]);  // Starting bankroll
        int goal  = Integer.parseInt(args[1]);  // Desired bankroll

        while (stake > 0 && stake < goal) {
            System.out.print("|");

            // Print out the spaces. Using a for loop and 
            //   printing a "*" if the counter variable 
            //   is equal to stake is good way to do it.

            // Flip a coin and increment/decrement the stake

            System.out.print("|\n");
        }
    }
}
于 2013-10-30T04:57:59.393 回答
1

这是一个可能更容易推理的破解解决方案:

import java.io.PrintStream;

public class SO {
  private static int gambleWithCaution(int stake) {
    if (Math.random() < 0.5) {
      return stake+1; // win $1
    } else {
      return stake-1;    // lose $1
    }
  }

  private static void renderStanding(int stake, int goal) {
    System.out.print('|');
    for(int dollar = 1; dollar< goal; dollar++) {
      if(dollar == stake) {
        System.out.print('*');
      } else {
        System.out.print(' ');
      }
    }
    System.out.println('|');
  }

  public static void main(String ... args) {
    int stake = Integer.parseInt(args[0]);    // gambler's stating bankroll
    int goal = Integer.parseInt(args[1]);    // gambler's desired bankroll

    while(stake > 0 && stake < goal) {
      renderStanding(stake, goal);
      stake = gambleWithCaution(stake);
    }
    System.out.println((stake > goal) ? "You Won!" : "You Lost");
  }
}

使用这些值35您将获得以下输出:

|  * |
| *  |
|*   |
| *  |
|  * |
|   *|
|  * |
|   *|
You Won!

现在已经将其分离出来,您可以从中获得一些乐趣,例如创建这样的赌博函数:

private static int gambleWithReclessAbandon(int stake, int goal, double abandon) {
  int onTheLine = (int)(Math.random() * (int)(stake * abandon));
  if(stake < (0.5)*goal) {
    //If you are at less than 50% of your goal then just put it all on the line
    //and let it ride :)
    onTheLine = stake;
  }
  if (Math.random() < 0.5) {
    return stake+onTheLine; // win $
  } else {
    return stake-onTheLine; // lose $
  }
}

可以这样调用:

//Gamble up to 80% of your current stake
stake = gambleWithReclessAbandon(stake, goal, 0.80);

使用相同的两个值,这是我在第一次通过时看到的(我非常接近:)):

|  * |
|  * |
|   *|
| *  |
|   *|
You Lost
于 2013-10-30T04:54:05.027 回答