翻转一枚硬币,并在连续出现四次正面时停止。

huangapple go评论80阅读模式
英文:

Flipping a coin and stopping when it lands heads 4 times in a row

问题

任务是反复抛硬币直到连续出现四次正面然后显示所有导致这种情况的结果我一直在收到我加入的最后一个错误消息以防万一出了问题我不知道我搞错了什么想知道是否有人能够帮助

    class Main {
      public static void main(String[] args) {
      
          int h = 2;
          int t = 1;
      
          int count = 0;

          int result;
 
          while (count <= 4)
          {
       
            result = (int)Math.random() * 2;
            if (result == 2)
            {
            count++;
            System.out.print("H ");
            }
            else if (result == 1)
            {
            count = 0;
            System.out.print("T ");
            }
            else
            System.out.println("error");
          }
      }
    }
英文:

The assignment is to flip a coin until four heads in a row are seen and display all the results leading up to that. I keep getting the last error message I put in just in case it fell through. I have no idea what I messed up and was wondering if someone was able to help.

class Main {
  public static void main(String[] args) {
  
      int h = 2;
      int t = 1;
  
      int count = 0;

      int result;

      while (count&lt;=4)
      {
   
        result = (int)Math.random()*2;
        if (result == 2)
        {
        count++;
        System.out.print(&quot;H &quot;);
        }
        else if (result == 1)
        {
        count=0;
        System.out.print(&quot;T &quot;);
        }
        else
        System.out.println(&quot;error&quot;);
      }
  }
}

答案1

得分: 2

(int)Math.random() * 2

等同于

((int)Math.random()) * 2

鉴于Math.random()返回至少为零但小于一的数字,您的表达式将始终为零。

加上括号:

(int) (Math.random() * 2)

但是,请同时查看条件语句中“result”的值:您永远不会生成2。

英文:
(int)Math.random() * 2

is the same as

((int)Math.random()) * 2

Given that Math.random() returns a number at least zero but less than one, your expression is always going to be zero.

Put in parentheses:

(int) (Math.random() * 2)

But then, also look at the values of result in your conditionals: you will never generate 2.

答案2

得分: 1

将其中的一部分翻译如下:

你需要加1以获得可能的数值为一或二的情况:

result = (int) (Math.random() * 2 + 1);
英文:

You need to add 1 to have possible values of one or two:

result = (int) (Math.random() * 2 + 1);

答案3

得分: 1

您可以使用 `Random` 类和布尔值

    Random random = new Random();
    int count = 0;
    while (count < 4) {
        if (random.nextBoolean()) {
          System.out.print("H");
          count++;
        } else {
            count = 0;
            System.out.print("T");
        }
    }
英文:

You can use the Randomclass and boolean

Random random = new Random();
int count = 0;
while (count &lt; 4) {
    if (random.nextBoolean()) {
      System.out.print(&quot;H&quot;);
      count++;
    } else {
        count = 0;
        System.out.print(&quot;T&quot;);
    }
}

答案4

得分: 0

由于Math.random()的结果介于0和1之间,将其强制转换为int类型将删除小数点后的数字,你将始终得到零作为答案。
以下代码将帮助您生成在min和max之间的随机数。

// 定义范围
int max = 2;
int min = 1;
int range = max - min + 1;
int rand = (int)(Math.random() * range) + min;

要了解这是如何工作的,您可以将最小可能值设为0,最大可能值设为0.99,并将两者都乘以任何范围,比如说20,答案仍将在1到20.8之间,由于它不是四舍五入而是直接强制转换,所以变为20。因此,这可以为您提供任何范围的随机数。

英文:

As the result of Math.random() is between 0 and 1 type casting it to int will remove the digits after decimal point and you'll always have zero as answer.
Below code will help you to generate a random number between min and max.

// define the range 
      int max = 2; 
      int min = 1; 
      int range = max - min + 1; 
      int rand = (int)(Math.random() * range) + min;

For an explanation of how this works you can put the min possible value of 0 and max possible of 0.99 and multiply both by any range, let's say 20 the answer will still be in between 1 to 20.8 which gets turned to 20 as it's not rounding off but directly type casting. Hence, this can give you a random number for any range.

huangapple
  • 本文由 发表于 2020年10月22日 04:07:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/64471006.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定