在 java 中掷两个骰子直到 dice1 大于 dice2 两次
Rolling two dices until dice1 is larger than dice2 two times in java
骰子的输出是随机的,我为此创建了一个函数。
我想记录连续两次让骰子 1 大于骰子 2 需要多少 "attempts"。所以当它连续两次变大时,我希望它打印("it took" + trials + "for d1 to be larger than d2 two times in a row")。到目前为止我有这个但卡住了:
public static int Dice() {
return (int)(Math.random() * 6.0) + 1;
}
public static void main(String[] args) {
int d1 = Dice();
int d2 = Dice();
int trials = 0;
for (int i = 0; i < 100000; i++) {
if (t1 > t2) {
trials++;
if (t1 > t2) {
trials++
}
}
}
System.out.println(trials);
}
}
做这样的事情(将以下内容视为伪代码):
public static int[] Dice() {
int[] diceThrownTwoTimes = new int[2];
diceThrownTwoTimes[0] = (int)(Math.random() * 6.0) + 1;
diceThrownTwoTimes[1] = (int)(Math.random() * 6.0) + 1;
return diceThrownTwoTimes;
}
public static void main(String[] args) {
int trials = 0;
for (int i = 0; i < 100000; i++) {
int[] d1 = Dice();
int[] d2 = Dice();
if (d1[0] > d2[0] && d1[1] > d2[1]) {
trials++;
}
}
System.out.println(trials);
}
编辑:
要连续两次获得 dice1 值大于 dice2 值的尝试,可以执行以下操作:
public static int[] Dice() {
int[] diceThrownTwoTimes = new int[2];
diceThrownTwoTimes[0] = (int)(Math.random() * 6.0) + 1;
diceThrownTwoTimes[1] = (int)(Math.random() * 6.0) + 1;
return diceThrownTwoTimes;
}
public static void main(String[] args) {
int trials = 0;
for (int i = 0; i < 100000; i++) {
int[] d1 = Dice();
int[] d2 = Dice();
if (d1[0] > d2[0] && d1[1] > d2[1]) {
break;
}else{
trials++;
}
}
System.out.println(trials);
}
我会选择
public static int Dice() {
return (int)(Math.random() * 6.0) + 1;
}
public static void main(String[] args) {
int trials = 0;
int count = 0;
while (count < 2) {
int d1 = Dice();
int d2 = Dice();
if (d1 > d2) {
++count;
} else {
count = 0;
}
++trials;
}
println("It took " + trials + " trials for d1 to be larger than d2 " + count + " times in a row.")
}
欢迎来到 SO!这应该工作。查看评论,了解您做错的几件事和一些解释。
public static int Dice() {
return (int)(Math.random() * 6.0) + 1;
}
public static void main(String[] args) {
// initialize variables to be updated in the loop
int trials = 0;
Boolean lastTimeD1WasGreater = false;
for (int i = 0; i < 100000; i++) {
// always increment the counter!
trials++;
// you had the wrong variable names here.
// also they must be INSIDE the loop or else they would always come out the same
int d1 = Dice();
int d2 = Dice();
if (d1 > d2) {
if (lastTimeD1WasGreater) {
// if it was greater both last time and this time then we're done!
System.out.println("it took " + trials + " trials.");
break;
} else {
// otherwise set this variable so we can be on the lookout next time.
lastTimeD1WasGreater = true;
}
} else {
// d1 was not greater, so we'll start looking for a pair in the next loop.
lastTimeD1WasGreater = false;
}
}
}
您可以在主函数中轻松完成此操作,除非需要使用外部函数来 return 随机。
public static void main(String[] args)
{
int d1 = 0;
int d2 = 0;
int trials = 0;
int d1bigger = 0;
Random rand = new Random();
while(d1bigger < 2)
{
d1 = rand.nextInt((6 - 1) + 1) + 1 ;
d2 = rand.nextInt((6 - 1) + 1) + 1 ;
if(d1 > d2)
{
d1bigger ++;
trials++;
}
if(d2 > d1)
{
d1bigger = 0;
trials++;
}
}
System.out.println("It took " + trials + " trials for d1 to be larger than d2 two times in a row");
}
d1连续两次大于d2用了7次
示例输出
这是我想出的
import java.util.Random;
public class 主要{
public static int Dice() {
return (int) (Math.random() * 6.0) + 1;
}
public static void main(String[] args) {
Random rand = new Random();
int trials = 0;
int twoTimes = 0;
do {
int d1 = rand.nextInt(25) + 1;
int d2 = rand.nextInt(25) + 1;
trials++;
if (d1 > d2) {
twoTimes++;
System.out.printf("%s time greater\nd1 was " + d1 + "\nd2 was " + d2 + "\n-------\n", twoTimes);
}
}
while (twoTimes != 2);
System.out.printf("\nIt took %s times", trials);
}
}
骰子的输出是随机的,我为此创建了一个函数。 我想记录连续两次让骰子 1 大于骰子 2 需要多少 "attempts"。所以当它连续两次变大时,我希望它打印("it took" + trials + "for d1 to be larger than d2 two times in a row")。到目前为止我有这个但卡住了:
public static int Dice() {
return (int)(Math.random() * 6.0) + 1;
}
public static void main(String[] args) {
int d1 = Dice();
int d2 = Dice();
int trials = 0;
for (int i = 0; i < 100000; i++) {
if (t1 > t2) {
trials++;
if (t1 > t2) {
trials++
}
}
}
System.out.println(trials);
}
}
做这样的事情(将以下内容视为伪代码):
public static int[] Dice() {
int[] diceThrownTwoTimes = new int[2];
diceThrownTwoTimes[0] = (int)(Math.random() * 6.0) + 1;
diceThrownTwoTimes[1] = (int)(Math.random() * 6.0) + 1;
return diceThrownTwoTimes;
}
public static void main(String[] args) {
int trials = 0;
for (int i = 0; i < 100000; i++) {
int[] d1 = Dice();
int[] d2 = Dice();
if (d1[0] > d2[0] && d1[1] > d2[1]) {
trials++;
}
}
System.out.println(trials);
}
编辑:
要连续两次获得 dice1 值大于 dice2 值的尝试,可以执行以下操作:
public static int[] Dice() {
int[] diceThrownTwoTimes = new int[2];
diceThrownTwoTimes[0] = (int)(Math.random() * 6.0) + 1;
diceThrownTwoTimes[1] = (int)(Math.random() * 6.0) + 1;
return diceThrownTwoTimes;
}
public static void main(String[] args) {
int trials = 0;
for (int i = 0; i < 100000; i++) {
int[] d1 = Dice();
int[] d2 = Dice();
if (d1[0] > d2[0] && d1[1] > d2[1]) {
break;
}else{
trials++;
}
}
System.out.println(trials);
}
我会选择
public static int Dice() {
return (int)(Math.random() * 6.0) + 1;
}
public static void main(String[] args) {
int trials = 0;
int count = 0;
while (count < 2) {
int d1 = Dice();
int d2 = Dice();
if (d1 > d2) {
++count;
} else {
count = 0;
}
++trials;
}
println("It took " + trials + " trials for d1 to be larger than d2 " + count + " times in a row.")
}
欢迎来到 SO!这应该工作。查看评论,了解您做错的几件事和一些解释。
public static int Dice() {
return (int)(Math.random() * 6.0) + 1;
}
public static void main(String[] args) {
// initialize variables to be updated in the loop
int trials = 0;
Boolean lastTimeD1WasGreater = false;
for (int i = 0; i < 100000; i++) {
// always increment the counter!
trials++;
// you had the wrong variable names here.
// also they must be INSIDE the loop or else they would always come out the same
int d1 = Dice();
int d2 = Dice();
if (d1 > d2) {
if (lastTimeD1WasGreater) {
// if it was greater both last time and this time then we're done!
System.out.println("it took " + trials + " trials.");
break;
} else {
// otherwise set this variable so we can be on the lookout next time.
lastTimeD1WasGreater = true;
}
} else {
// d1 was not greater, so we'll start looking for a pair in the next loop.
lastTimeD1WasGreater = false;
}
}
}
您可以在主函数中轻松完成此操作,除非需要使用外部函数来 return 随机。
public static void main(String[] args)
{
int d1 = 0;
int d2 = 0;
int trials = 0;
int d1bigger = 0;
Random rand = new Random();
while(d1bigger < 2)
{
d1 = rand.nextInt((6 - 1) + 1) + 1 ;
d2 = rand.nextInt((6 - 1) + 1) + 1 ;
if(d1 > d2)
{
d1bigger ++;
trials++;
}
if(d2 > d1)
{
d1bigger = 0;
trials++;
}
}
System.out.println("It took " + trials + " trials for d1 to be larger than d2 two times in a row");
}
d1连续两次大于d2用了7次
示例输出
这是我想出的
import java.util.Random;
public class 主要{
public static int Dice() {
return (int) (Math.random() * 6.0) + 1;
}
public static void main(String[] args) {
Random rand = new Random();
int trials = 0;
int twoTimes = 0;
do {
int d1 = rand.nextInt(25) + 1;
int d2 = rand.nextInt(25) + 1;
trials++;
if (d1 > d2) {
twoTimes++;
System.out.printf("%s time greater\nd1 was " + d1 + "\nd2 was " + d2 + "\n-------\n", twoTimes);
}
}
while (twoTimes != 2);
System.out.printf("\nIt took %s times", trials);
}
}