簡體   English   中英

我正在嘗試使三個骰子滾動並顯示其輸出,但是在嘗試弄清楚它時遇到了麻煩。

[英]I'm trying to make three dice roll and have their output show, but I am having trouble trying to figure it out.

我正在嘗試使我的程序能夠顯示直到3個骰子降落在相同數量上所花費的次數,然后告訴使用它花費了多少次,它已經降落的次數並讓他們重復一次處理3次。 但是,我不知道如何使程序正常工作。 當前,僅彈出“嘗試”,“骰子1”,“骰子2”,“骰子3”,然后在“嘗試”下進行的擲骰數量。 如何使該程序的其余部分起作用? 謝謝!

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

添加一個計數器,僅在獲得匹配項時計數。 這樣一來,您只計算匹配的次數。 對於while,將條件更改為while <3。對於打印,可以將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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM