骰子模拟找到正确的点

时间:2018-01-04 11:35:00

标签: java

  你想要多少骰子点?
    6
  尝试1:2
  尝试2:4
  尝试3:5
  尝试4:6
  在4尝试你发现6点

所以这就是我想要的输出,但似乎无法找到。

  public abstract class Bar
  {
    public int MemberA {get;set;}
    public string MemberB {get;set;}
  }

  public interface IBar
  {
    public int MemberA {get;set;}
    public string MemberB {get;set;}
  }

这就是我所拥有的。我现在尝试了一个for循环,但它似乎不起作用。有人能帮助我吗?

2 个答案:

答案 0 :(得分:1)

请下次正确格式化您的问题,这有助于我们为您提供帮助:)

你需要一个while循环来掷骰子。

int dots = s.nextInt();
boolean found = false;
Random rnd = new Random();
while(!found) {
   int rolled= rnd.nextInt(6) + 1;
   if(rolled == dots) // do your stuff
        found = true;
}

希望它有所帮助。

答案 1 :(得分:1)

你可以通过这种方式达到你想要的目标:

Scanner scan = new Scanner (System.in);

    int randomValue = 0;   //this is the random value that'd be autogenerated in each loop
    int counter = 1;    //this represents the number of trials

    System.out.println("how many dice dots do u want?");
    int dotsWanted = scan.nextInt();

    while(randomValue != dotsWanted){
        randomValue = (int) (6 * Math.random()) + 1;
        counter++;
    }

    System.out.printf("In %d trials, %d dots were found\n", counter, dotsWanted);

我希望这有帮助..快乐编码!