我是Java新手,需要帮助。我被要求编写一个掷骰子的程序,并确定玩家在骰子上面获得两个“1”的机会。我有不同的功能,如角色(),getTopFace()等我想要得到骰子上使用这些函数的数字,但不知道如何在我的主函数中调用它们。这里是我的代码:如何通过方法传递信息并返回值
import javax.swing.JOptionPane;
import java.util.Random;
public class SnakeEyes {
private final int sides;
private int topFace;
public static void main(String[]args)
{
String numberSides;
int n;
numberSides=JOptionPane.showInputDialog("Please enter the number of sides on the dice:");
n = Integer.parseInt (numberSides);
int[]die=new int[n];
for (int index=0; index<n;index++)
{
die[index]=index+1;
}
//Here is where I want to get information from my functions and calculate the ods of getting two 1's.
}
public void Die(int n)
{
if(n>0)
{
int sides=n;
topFace=(int)(Math.random()*sides)+1;
}
else{
JOptionPane.showMessageDialog(null, " Die : precondition voliated");
}
}
public int getTopFace(int topFace)
{
return topFace;
}
public int role(int[] die)
{
topFace=(int)(Math.random()*sides)+1;
return topFace;
}
}
为什么'role()'函数需要'topFace'参数? –
对不起,这实际上是一个错字。你能帮助解决我的问题吗? – user3241910
'private final int sides;'永远不会初始化(在构造函数中)。所以它的价值永远是0. –