使用 math.random 方法来创建掷骰子方法。

huangapple go评论146阅读模式
英文:

using math.random method to create dice method

问题

import java.util.Scanner;

public class Lab06d {
    public static double money(double userMoney, int option, double bet) {
        // variables
        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 && dice2 == 1) || (dice1 == 6 && dice2 == 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.nextDouble();
        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.nextDouble();
        System.out.println(money(userMoney, option, bet));
        userMoney = money(userMoney, option, bet);
    }
}
英文:

ı basically created a program for a bet game and ı wanted to use math.random method to obtain possible face values.

my question is that math.random is always same value so ı cannot use it repeatedly.

how can ı use math.random method repeatedly. ı dont want to get same value for my dice;

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);  
}
}

答案1

得分: 1

(int) Math.random() * 6 + 1

与以下表达式等价

((int) Math.random()) * 6 + 1.

首先进行向下取整然后进行乘法和加法运算
由于 Math.random() 返回的是介于 0 <= x < 1 的数它总是被转换为 0
注意(int) 总是向下取整因此 (int) 3.999 会变成 3(int) -3.9 会变成 -4

可以使用以下方式添加括号

(int) (Math.random() * 6 + 1)

你还可以将其封装为一个函数如下

static int throwDice() {
    return (int) (Math.random() * 6 + 1);
}

Java 中的运算符具有特定的优先级就像数学中的乘法优先于加法一样除非使用括号你可以在[这里][1]查阅运算符的优先级如果有疑问或为了澄清),请使用括号

  [1]: https://introcs.cs.princeton.edu/java/11precedence/
英文:
(int) Math.random() * 6 + 1

means the same as

((int) Math.random())    *    6   +   1. 

You're first rounding down, then multiplying and adding.
As Math.random() returns as number 0 <= x < 1, it is always casted to 0.
Notice that (int) always rounds down, so (int) 3.999 would become 3 for example, as well as (int)-3.9 would become -4.

Add braces like this:

 (int)     (Math.random() * 6 + 1)

You can also wrap it into a function like this:

static int throwDice() {
return (int) (Math.random() * 6 + 1);
}

Operators in Java have a specific precedence to them, like multiplication comes before addition in mathematics, unless you use braces. You can look it up here. When in doubt (or to clarify), use braces.

答案2

得分: 0

问题出在初始化骰子1和骰子2的时候。您必须记住,不能将基本类型按引用传递给方法。因此,您必须将完整的数据进行封装,本例中为Math.random()*6+1,并解析为整数类型。因此,代码如下:

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

英文:

The problem is at the time of initializing dice 1, dice 2. You must remember that you can't pass a primitive type by reference to a method. So you must wrap the COMPLETE DATA, which in this case is Math.random()*6+1 and parse the integer type. So, the code would be

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

huangapple
  • 本文由 发表于 2020年4月4日 06:26:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/61021334.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定