英文:
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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论