我试图做三个骰子滚动,并显示他们的输出,但我试图找出它的麻烦。

问题描述:

我想让我的程序能够显示直到3个骰子落在同一个数字上需要花费多少次的次数,然后告诉使用次数,它使用了多少次,它已经降落并拥有它们重复这个过程3次。但是,我无法弄清楚如何使程序工作。目前,只有尝试,骰子1,骰子2,骰子3弹出,然后在Try下花费的金额。我如何使该程序的其余部分工作?谢谢!我试图做三个骰子滚动,并显示他们的输出,但我试图找出它的麻烦。

import java.util.Random; 
public class Q1 
    { 
    public static void main(String[] args){ 
     Random g=new Random(); 
    boolean same=false; 
    int dice1=0; 
    int dice2=0; 
    int dice3=0; 
    int count=0; 
    System.out.println("Try\tDice 1\tDice 2\tDice 3"); 
    do{ 
      System.out.println(); 
      count++; 
      dice1=g.nextInt(6)+1; 
      dice2=g.nextInt(6)+1; 
      dice3=g.nextInt(6)+1; 
      System.out.print(count+"\t"); 

      if(dice1==dice2 && dice2==dice3){ 
      same=true; 
      System.out.println("It took"+count+"times and they all landed on number "+??); 
      } 
     } 
     while(!same); 
    } 
} 

添加一个计数器,只有当你得到你的匹配时才算。这样你只计算它匹配的次数。 对于同时,改变条件,同时对于打印,您可以添加dice1到的System.out.println结束

int count=0; 
int matchCount = 0; 
System.out.println("Try\tDice 1\tDice 2\tDice 3"); 
do{ 
     System.out.println(); 
     count++; 
     dice1=g.nextInt(6)+1; 
     dice2=g.nextInt(6)+1; 
     dice3=g.nextInt(6)+1; 
     System.out.print(count+"\t"); 

     if(dice1==dice2 && dice2==dice3){ 
     same=true; 
     matchCount++; 
     System.out.println("It took"+count+"times and they all landed on number "+dice1); 
     } 
    } 
    while(matchCount < 3);