我正在进行赌博模拟,我无法弄清楚为什么End Bank有时低于零或高于250.我在while循环之后包含一个for循环重复模拟100次。 请记住我从文本文件中读取的变量。并且while循环仅在赌徒超过0美元且低于250时运行,但最终银行高于或低于某一时间。
有什么想法吗? 这是我的代码:
import java.io.*;
import java.util.Scanner;
public class GamblersRuin {
public static int bank;
public static int goal;
public static int beta;
public static double prob;
public static int numberofb;
public static String yesn;
public static int rounds;
public static int newbank;
public static String s = ("stuff");
public static void main(String[] args) throws FileNotFoundException {
Scanner scan = new Scanner(System.in);
Scanner file = new Scanner(new File("numbers.txt"));
//Variables
bank = file.nextInt();
goal = file.nextInt();
beta = file.nextInt();
prob = file.nextDouble();
numberofb = 0;
String yesn = file.next();
rounds = file.nextInt();
newbank = bank;
//Logic
while(bank > 0 && bank < goal) {
for(int i = 0; i <= rounds; i++) {
bank -= beta;
//Win
if(Math.random() < prob) {
bank += (1/prob) * 1;
numberofb += 1;
}
if(yesn.equals("Y")) {
for(int y = 0; y < bank; y++) {
System.out.print("*");
}
System.out.println();
}
}
}
//Main Output
System.out.print("==Gambling Simulation==\n");
System.out.printf("Starting Bank: " + bank + "\n");
System.out.printf("Goal: 250.00\n");
System.out.printf("Bet Amount: 1.00\n");
System.out.printf("Probability: 50%%\n");
System.out.printf("========Results========\n");
System.out.printf("Number of Bets:%9s", numberofb + numberofb + "\n");
System.out.printf("End Bank:%14s\n", bank);
}
}
答案 0 :(得分:0)
添加此If条件,以便程序从100次模拟中出来,如果它小于或等于0或250或更高。
for(int i = 0; i <= rounds; i++) {
if(bank < 1 || bank >= goal)
{
break;
}
bank -= beta;
请记住,100次模拟可能不会让玩家失去所有信用或因为概率而超过250,所以程序需要保持在while循环中直到玩家击中目标并退出for环。 HTH。