Java 赋值与随机

Java 赋值与随机,java,Java,我试图写一个方法,反复地抛硬币,直到看到三个头像排成一行。每次抛硬币时,所看到的东西都会显示出来(H表示正面,T表示反面)。当连续三个头被翻转时,会打印一条祝贺信息。 T T H 一排三个头 public static void threeHeads(){ Random rnd=new Random(); char c = (char) (rnd.nextInt(26) + 'a'); for(int i=1;i<=c. } 另一个问题是赋值,它是==和等于。 对于布

我试图写一个方法,反复地抛硬币,直到看到三个头像排成一行。每次抛硬币时,所看到的东西都会显示出来(H表示正面,T表示反面)。当连续三个头被翻转时,会打印一条祝贺信息。 T T H 一排三个头

public static void threeHeads(){
  Random rnd=new Random();
    char c = (char) (rnd.nextInt(26) + 'a');
    for(int i=1;i<=c.
}
另一个问题是赋值,它是==和等于。 对于布尔值,我使用== 我知道对于字符串,我应该使用equal。那么对于char字符,我应该使用什么呢? 例如,char=='y'
我说得对吗

  • 掷硬币的结果是二元的。将H匹配到1,将T匹配到0。您仅随机生成这两个数字
  • 在循环中放置一个计数器
    cnt
    ,当它是T(0)时,计数器将设置为0;如果它是H(1),计数器将设置为
    cnt++
    。然后,如果
    cnt>2
    (类似于
    if(cnt>2)break;
    ),则必须中断循环

  • 不要忘记,每次通过循环时都需要重新生成随机数。在当前代码中,它只执行一次

  • 我认为这些想法应该足以编写您的代码

  • 掷硬币的结果是二元的。将H匹配到1,将T匹配到0。您仅随机生成这两个数字
  • 在循环中放置一个计数器
    cnt
    ,当它是T(0)时,计数器将设置为0;如果它是H(1),计数器将设置为
    cnt++
    。然后,如果
    cnt>2
    (类似于
    if(cnt>2)break;
    ),则必须中断循环

  • 不要忘记,每次通过循环时都需要重新生成随机数。在当前代码中,它只执行一次


  • 我认为这些想法应该足以编写您的代码。

    我认为这是一个家庭作业

    不要使用
    Random.nextInt
    ,而是使用
    Random.nextBoolean

    TAIL
    false
    HEAD
    true

    然后,您需要一个一行中的头计数器,该计数器在打开新的时递增,在翻转时重置为
    0


    一旦该计数器的值为
    3
    ,就有了循环的退出条件。

    我假设这是一个家庭作业

     int head =0;
     int tail =1;
     public static void threeHeads(){
          Random rnd=new Random();
          int headsSeen = 0;
           while(headsSeen < 3){
             int res = rnd.nextInt(1); //returns 1 or 0
             if (res == head){
                  headsSeen ++; 
             }else{
                  headsSeen = 0; //there was a tail so reset counter
             }
               }
             ///At this point three heads seen in a row
    }
    
    不要使用
    Random.nextInt
    ,而是使用
    Random.nextBoolean

    TAIL
    false
    HEAD
    true

    然后,您需要一个一行中的头计数器,该计数器在打开新的时递增,在翻转时重置为
    0

    一旦该计数器的值为
    3
    ,就有了循环的退出条件。

    int head=0;
    
     int head =0;
     int tail =1;
     public static void threeHeads(){
          Random rnd=new Random();
          int headsSeen = 0;
           while(headsSeen < 3){
             int res = rnd.nextInt(1); //returns 1 or 0
             if (res == head){
                  headsSeen ++; 
             }else{
                  headsSeen = 0; //there was a tail so reset counter
             }
               }
             ///At this point three heads seen in a row
    }
    
    int-tail=1; 公共静态无效三头(){ 随机rnd=新随机(); int=0; 而(见<3){ int res=rnd.nextInt(1);//返回1或0 如果(res==头部){ 头戴眼镜++; }否则{ headsSeen=0;//有一个尾部,因此重置计数器 } } ///在这一点上,可以看到三个头排成一行 }
    int head=0;
    int-tail=1;
    公共静态无效三头(){
    随机rnd=新随机();
    int=0;
    而(见<3){
    int res=rnd.nextInt(1);//返回1或0
    如果(res==头部){
    头戴眼镜++;
    }否则{
    headsSeen=0;//有一个尾部,因此重置计数器
    }
    }
    ///在这一点上,可以看到三个头排成一行
    }
    
    通常,每当你问自己“如何跟踪XXX”,答案就是声明一个新变量。但是,在您的情况下,可以使用循环计数器
    i

    以下是我将如何处理这个问题:

    public static void threeHeads()
    {
        Random rnd=new Random();
        char c; //no need to initialize the char
    
        //ostensibly, we will loop 3 times
        for(int i=0; i < 3; i ++)
        {
              c = rnd.nextBoolean() ? 'h' : 't'; /*get random char*/;
    
              if (c != 'h')
              {
                   //but if we encounter a tails, reset the loop counter to -1
                                 //that way it will be 0 next time the loop executes
                   i = -1;
              }
    
              System.out.println(c);
         }
    }
    
    publicstaticvoidthreeheads()
    {
    随机rnd=新随机();
    char c;//不需要初始化字符
    //表面上,我们将循环3次
    对于(int i=0;i<3;i++)
    {
    c=rnd.nextBoolean()?'h':'t';/*获取随机字符*/;
    如果(c!=“h”)
    {
    //但是如果我们遇到一个尾巴,将循环计数器重置为-1
    //这样,下次循环执行时它将为0
    i=-1;
    }
    系统输出打印ln(c);
    }
    }
    
    这样,它将继续尝试循环三次,直到每次
    c
    都是
    'h'

    要回答您关于
    ==
    equals()
    的问题,请执行以下操作:


    您始终可以对基本类型(int、char、double、任何非对象的对象)使用
    ==
    。对于对象(字符串、双大写D、列表),最好使用
    equals
    。这是因为
    ==
    将测试对象是否是完全相同的对象——只有当它们在内存中占据相同的位置时才是正确的。实际上,十有八九的情况下,你会对检查对象是否相等感兴趣,而不在乎它们是否是同一个对象。了解这方面的细节仍然是一个好主意,但是,如果您遇到一种情况,您确实想使用
    =

    一般来说,每当您发现自己问“如何跟踪XXX”,答案是声明一个新变量。但是,在您的情况下,可以使用循环计数器
    i

    以下是我将如何处理这个问题:

    public static void threeHeads()
    {
        Random rnd=new Random();
        char c; //no need to initialize the char
    
        //ostensibly, we will loop 3 times
        for(int i=0; i < 3; i ++)
        {
              c = rnd.nextBoolean() ? 'h' : 't'; /*get random char*/;
    
              if (c != 'h')
              {
                   //but if we encounter a tails, reset the loop counter to -1
                                 //that way it will be 0 next time the loop executes
                   i = -1;
              }
    
              System.out.println(c);
         }
    }
    
    publicstaticvoidthreeheads()
    {
    随机rnd=新随机();
    char c;//不需要初始化字符
    //表面上,我们将循环3次
    对于(int i=0;i<3;i++)
    {
    c=rnd.nextBoolean()?'h':'t';/*获取随机字符*/;
    如果(c!=“h”)
    {
    //但是如果我们遇到一个尾巴,将循环计数器重置为-1
    //这样,下次循环执行时它将为0
    i=-1;
    }
    系统输出打印ln(c);
    }
    }
    
    这样就可以了