gpt4 book ai didi

java - Diceroll 概率方法结构

转载 作者:行者123 更新时间:2023-12-02 07:05:29 26 4
gpt4 key购买 nike

我遇到的问题是非常基本的,但是我还没有很好地理解这一点。下面的程序使用递归来计算给定数量的骰子(由用户输入)总计为用户选择的数字的概率。

据我了解,方法 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)

最佳答案

Java 中的所有代码都需要包含在方法或类中。您不能只调用 float 在类中间的 DiceRoll

您真正想做的是从用户那里获取输入,就像您当前一样,然后在 main 方法中调用 DiceRoll

关于java - Diceroll 概率方法结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16180448/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com