Java 使用math.random方法创建骰子方法

Java 使用math.random方法创建骰子方法,java,Java,ı基本上为赌博游戏创建了一个程序,并希望使用math.random方法获得可能的面值 我的问题是math.random总是相同的值,所以ı不能重复使用它 如何反复使用数学随机方法。ı不想让我的骰子得到同样的价值 import java.util.Scanner; public class Lab06d { public static double money(double userMoney, int option, double bet ) { //variable i

ı基本上为赌博游戏创建了一个程序,并希望使用math.random方法获得可能的面值

我的问题是math.random总是相同的值,所以ı不能重复使用它

如何反复使用数学随机方法。ı不想让我的骰子得到同样的价值

import java.util.Scanner;
public class Lab06d
{
  public static double money(double userMoney, int option, double bet )
  {
    //variable
    int dice1;
    int dice2;
    //codes
    dice1 = (int) Math.random() * 6 + 1;
    dice2 = (int) Math.random() * 6 + 1;
    System.out.println("dice sum: " + dice1 + dice2 );
    if(option == 1)
    {
      if((dice1 + dice2) % 2 == 1)
        userMoney = userMoney + ( bet / 2); 
      else
        userMoney = userMoney - ( bet / 4);
    }
    if(option == 2)
    {
      if((dice1 + dice2) % 2 == 1)
        userMoney = userMoney - ( bet / 4); 
      else
        userMoney = userMoney + ( bet / 2);
    }
    if(option == 3)
    {
      if((dice1 == 1 && dice1 == 1) || (dice1 == 6 && dice1 == 6))
        userMoney = userMoney + ( bet); 
      else
        userMoney = userMoney - ( bet);
    }
    return userMoney;
  }
  public static void main(String[] args)
  {
    // variables
    double userMoney;
    int option;
    double bet;
    //codes
    Scanner scan = new Scanner (System.in);


    System.out.println("enter your money: ");
    userMoney = scan.nextInt();
    System.out.println("enter your option 1 for odd 2 for even 3 for extreme: ");
    option = scan.nextInt();
    System.out.println("enter your bet: ");
    bet = scan.nextInt(); 
    System.out.println(money(userMoney, option, bet));
    userMoney = money(userMoney, option, bet);  
  }
}
意思与

((int) Math.random())    *    6   +   1. 
首先进行四舍五入,然后进行乘法和加法。

由于Math.random()以数字0的形式返回问题出现在初始化骰子1、骰子2时。必须记住,不能通过引用方法来传递基元类型。因此,您必须包装完整的数据,在本例中是Math.random()*6+1并解析整数类型。所以,代码应该是

dice1=(int)(Math.random()*6+1);
dice2=(int)(Math.random()*6+1)

不要使用
Math.random()
,而是使用
random
对象。没问题。请点击左边的灰色箭头接受我的答案,这样其他人也可以找到。当然,它有时间限制。2分钟后,我来做
((int) Math.random())    *    6   +   1. 
 (int)     (Math.random() * 6 + 1)
static int throwDice() {
    return (int) (Math.random() * 6 + 1);
}