我目前正在学习Java,我知道Python很好,但是我似乎无法使Java问题与我的python程序一样工作。这是python程序:
import random as rand
successes = 0
for 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 = 19832
stake = 200 bets made = 5240
stake = 200 bets made = 3156
stake = 0 bets made = 10338
stake = 200 bets made = 11198
stake = 0 bets made = 6150
stake = 200 bets made = 10934
stake = 200 bets made = 11368
stake = 0 bets made = 8724
stake = 0 bets made = 17158
stake = 200 bets made = 6158
stake = 200 bets made = 7690
stake = 200 bets made = 2170
stake = 200 bets made = 12268
stake = 0 bets made = 15878
stake = 200 bets made = 16486
stake = 0 bets made = 6396
0.518
6.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);
}
}
}
当我运行这个时,我会得到这个
stake = 196.0 bets made = 10290.0
stake = 197.0 bets made = 10291.0
stake = 198.0 bets made = 10292.0
stake = 197.0 bets made = 10293.0
stake = 196.0 bets made = 10294.0
stake = 197.0 bets made = 10295.0
stake = 198.0 bets made = 10296.0
stake = 197.0 bets made = 10297.0
stake = 198.0 bets made = 10298.0
stake = 197.0 bets made = 10299.0
stake = 196.0 bets made = 10300.0
stake = 195.0 bets made = 10301.0
stake = 196.0 bets made = 10302.0
stake = 197.0 bets made = 10303.0
stake = 196.0 bets made = 10304.0
stake = 197.0 bets made = 10305.0
stake = 196.0 bets made = 10306.0
stake = 197.0 bets made = 10307.0
stake = 198.0 bets made = 10308.0
stake = 199.0 bets made = 10309.0
stake = 200.0 bets made = 10310.0
average successes = 0.512
average bets = 10.31
答案 0 :(得分:2)
在python中,如果stake == 200:
在while
循环之外,则在java中它在内部,同样,当您在python之外,java中的输出在for
循环中。我相信这更接近您的python代码。
public class Gambler {
public static void main(String[] args){
double successes = 0;
double bets = 0;
for (int i = 0; i <1000;i++){
double stake = 100;
bets = 0; // initialize outside of for loop
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);
}
}
答案 1 :(得分:-1)
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);
}
}