在我的作业中,我必须制作一个简单版本的胡扯,由于某种原因,即使两个变量都不为 0,百分比赋值总是产生 0,这是代码。
import java.util.Random;
标题,注意变量
public class Craps {
private int die1, die2,myRoll ,myBet,point,myWins,myLosses;
private double winPercent,lossPercent;
private Random r = new Random();
只需滚动两个模具并生产它们的一些。
public int roll(){
die1 = r.nextInt(6)+1;
die2 = r.nextInt(6)+1;
return(die1 + die2);
}
Play 方法,这只是循环游戏。
public void play(){
myRoll = roll();
point = 0;
if(myRoll == 2 ||myRoll == 3 || myRoll == 12){
System.out.println("You lose!");
myLosses++;
}else if(myRoll == 7 || myRoll == 11){
System.out.println("You win!");
myWins++;
}else{
point = myRoll;
do {
myRoll = roll();
}while(myRoll != 7 && myRoll != point);
if(myRoll == point){
System.out.println("You win!");
myWins++;
}else{
System.out.println("You lose!");
myLosses++;
}
}
}
这就是错误所在,这是测试方法。
public void tester(int howMany){
int i = 0;
while(i < howMany){
play();
i++;
}
错误就在这些赋值语句中
winPercent = myWins/i * 100;
lossPercent = myLosses/i* 100;
System.out.println("program ran "+i+" times "+winPercent+"% wins "+ lossPercent+"% losses with "+myWins+" wins and "+myLosses+" losses");
}
}