我目前正在学习 Java,我对 Python 了解得很好,但我似乎无法让我的 Java 问题以与我的 Python 程序相同的方式工作。这是python程序:import random as randsuccesses = 0for x in range (1000): stake = 100 bets = 0 while stake >0 and stake <200: play = rand.randint(0,1) bets +=1 if play == 0: stake = stake -1 else: stake = stake + 1 if stake == 200: successes +=1 print ('stake = ', stake, 'bets made =', bets)print (successes/1000)print(bets/1000)当我运行它时,我得到这样的东西:stake = 0 bets made = 19832stake = 200 bets made = 5240stake = 200 bets made = 3156stake = 0 bets made = 10338stake = 200 bets made = 11198stake = 0 bets made = 6150stake = 200 bets made = 10934stake = 200 bets made = 11368stake = 0 bets made = 8724stake = 0 bets made = 17158stake = 200 bets made = 6158stake = 200 bets made = 7690stake = 200 bets made = 2170stake = 200 bets made = 12268stake = 0 bets made = 15878stake = 200 bets made = 16486stake = 0 bets made = 63960.5186.396现在这是我为我的 java 程序所做的事情 我被告知使用 Math.random() 和 Math.Round() 我认为这可能是我遇到问题的地方我不知道把 Math.Round 放在哪里()public class Gambler { public static void main(String[] args){ double successes = 0; for (int i = 0; i <1000;i++){ double stake = 100; double bets = 0; while(stake>0 && stake <200){ bets++; if (Math.random()<0.5) stake ++; else stake --; if (stake==200) successes++; System.out.println("stake = "+ stake + " bets made = " + bets); } System.out.println("average successes = " + successes/1000); System.out.println("average bets = " + bets/1000); } } }
2 回答
猛跑小猪
TA贡献1858条经验 获得超8个赞
public class Gambler {
public static void main(String[] args){
double successes = 0;
for (int i = 0; i <1000;i++){
double stake = 100;
double bets = 0;
double play = Math.random();
while(stake>0 && stake <200){
bets++;
if (play == 0) stake --;
else stake ++;
}
if (stake==200) successes++;
System.out.println("stake = "+ stake + " bets made = " + bets);
}
System.out.println("average successes = " + successes/1000);
System.out.println("average bets = " + bets/1000);
}
}
添加回答
举报
0/150
提交
取消