我遇到的问题是非常基本的,但是这是我没有很好地掌握的东西。下面的程序使用递归来计算给定数量的骰子(由用户输入)总计为用户选择的数字的概率。Diceroll概率方法结构
据我所知,方法DiceRoll是Diceroll类的一部分。但是,当我尝试调用该方法时,出现错误。我相信这个计划的结构有一些根本性的错误。有人可以帮我吗?
import java.util.Scanner;
public class DiceRoll {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
double dice = 0;
int r = 0;
System.out.println("Please input the number of dice you wish to roll between 0 and 25: ");
if (in.nextInt() < 0 || in.nextInt() > 25){
System.out.println("invalid number of dice");
} else {
dice = in.nextInt();
}
System.out.println("What number between 0 and 125 do you wish to roll?");
if (in.nextInt() < 0 || in.nextInt() > 125) {
System.out.println("invalid number, please choose between 0 and 125");
} else {
r = in.nextInt();
}
}
double DiceRoll(double dice,int r) {
if (dice==1 && (r<1 || r>6)){
return 0;
}
if (dice==1 && (r>=1 && r<=6)){
return (1.0/6);
} else {
return ((1.0/6)*DiceRoll(dice-1,r-1));
}
}
}
DiceRoll(dice, r)
您至少可以尝试格式化您的问题。 – 2013-04-23 22:49:48
我的道歉,猎人。我会尝试编辑它。 – purpscurp 2013-04-23 22:50:57
@MrD对不起,我已经编辑过这个问题。 – 2013-04-23 22:54:26