关闭。这个问题需要更多 focused .它目前不接受答案。
想改进这个问题?更新问题,使其仅关注一个问题 editing this post .
8年前关闭。
Improve this question
我正在尝试通过一种方法为 int 生成一个随机数。我知道这是无法做到的,因为 int 是原始类型,并且我得到一个 int cannot be deferenced 错误。它周围有没有我仍然可以使用该方法来获取 int 值的地方?
public int move()
{
Random random = new Random();
int generatedNum = random.nextInt(7 - 1) + 1;
return generatedNum;
}
public static void main(String[] args)
{
int player1 = 0;
int player2 = 0;
player1 = player1.move();
player2 = player2.move();
}
最佳答案
你想做什么?
这会起作用,但不知道它是否是你想要的......
// made this static so you don't need a class reference
public static int move()
{
Random random = new Random();
int generatedNum = random.nextInt(7 - 1) + 1;
return generatedNum;
}
public static void main(String[] args)
{
int player1 = 0;
int player2 = 0;
// Now we just call the static method move to set the ints.
player1 = move();
player2 = move();
}
https://stackoverflow.com/questions/15799997/