随机投币输出次数运行

Random Coin Flipper output number of times run

我正在尝试在 java 中编写一个程序,它可以抛一枚假想的硬币并输出翻转结果,然后当某一面被抛 3 次时,它会停止并告诉你它抛了多少次.我的程序似乎无法运行

我的代码如下:

 import java.util.*;
public class FlipperThree {
    public static void main(String[] args) { 
        boolean fin = false;
        while(!fin){
            System.out.println("Welcome to Flipper!");
            int h = 0;
            int hcount = 0;
            int tcount = 0;
            int ocount = 0;
            String random;
            String[] ht;
            boolean done = false;
            while(!done){
                for(hcount<3||tcount<3){ht = new String[] {"Heads","Tails"};
                Random r =new Random();
                random = ht[r.nextInt(ht.length)];
                System.out.println(random);
                }
                if (hcount!=3||tcount!=3){
                    if(random == ht[h]){
                        hcount++;
                        ocount++;
                        tcount = 0;
                    }else{
                        hcount = 0;
                        tcount++;
                        ocount++;
                    }
                }else{
                    System.out.println("BINGO!That only took " + ocount+" flips to get 3 in a row!");
                    done = true;
                }
            }
        }fin = true;
    }
}

删除 for 并初始化 ht 一次并删除您的第一个 while 并将 || 更改为 &&:

import java.util.*;
public class FlipperThree {
    public static void main(String[] args) { 

            System.out.println("Welcome to Flipper!");
            int h = 0;
            int hcount = 0;
            int tcount = 0;
            int ocount = 0;
            String random;
            String[] ht = new String[] {"Heads","Tails"};
            boolean done = false;

            while(!done){
                Random r =new Random();
                random = ht[r.nextInt(ht.length)];
                System.out.println(random);

                if (hcount!=3 && tcount!=3){
                    ocount++;

                    if(random == ht[h]){
                        hcount++;
                        tcount = 0;
                    }else{
                        hcount = 0;
                        tcount++;
                    }
                }else{
                    System.out.println("BINGO!That only took " + ocount+" flips to get 3 in a row!");
                    done = true;
                }

    }
}
  1. 一般来说,您的程序比必要的要复杂得多。你只需要一个循环

  2. 您正在使用 == 来比较字符串。而不是 random == ht[h] 你应该说 random.equals(ht[h])

  3. 不要在 头或尾计数小于 3 时循环,而只想在 both[=32 时继续循环=] 少于三个。所以不是 hcount!=3 || tcount!=3 它是 hcount!=3 && tcount!=3

.

 public class FlipperThree {

     public static void main(String[] args) { 
         System.out.println("Welcome to Flipper!");
         int h = 0;
         int hcount = 0;
         int tcount = 0;
         int ocount = 0;
         String[] ht = new String[] {"Heads","Tails"};
         Random r =new Random();

         while (hcount < 3 && tcount < 3) {
             String random = ht[r.nextInt(ht.length)];
             System.out.println(random);
             if(random.equals(ht[h])){
                 hcount++;
                 ocount++;
                 tcount = 0;
             }
             else{
                 hcount = 0;
                 tcount++;
                 ocount++;
             }
         }
         System.out.println("BINGO!That only took " + ocount+" flips to get 3 in a row!");
     }
 }

$ java FlipperThree
Welcome to Flipper!
Tails
Heads
Heads
Tails
Heads
Heads
Tails
Tails
Tails
BINGO!That only took 9 flips to get 3 in a row!

当您意识到您只需要跟踪连续硬币面的数量时,您可以使用更简单的代码来实现这一点。不管是什么脸:

/**
 * return:
 *    the number of coin tosses until you get the same face n consecutive times
 */
static int numTossesUntilNConsecutiveFaces(int n) {
    Random toss = new Random();
    // This counts the number of consecutive faces following a given face
    // For example:
    // for sequence 1 0, numConsecutive == 0
    // for sequence 1 1, numConsecutive == 1
    // for sequence 0 0 0, numConsecutive == 2
    int numConsecutives = 0;
    int numTosses = 0;
    // Initialize the lastCoin value as -1, so the first coin won't be equal to it
    int lastCoin = -1;
    do {
        // The result of the toss will be 0 or 1 (head or tails)
        int coin = toss.nextInt(2);
        if (coin == lastCoin) {
            // If the current coin is the same as the last coin, increment the counter
            numConsecutives++;
        } else {
            // If it is different, reset the counter
            numConsecutives = 0;
        }
        // Make this coin the last coin
        lastCoin = coin;
        numTosses++;
    // Stop when the number of consecutives is n - 1.
    // For n == 3, that is when the last three numbers were 1 1 1 or 0 0 0
    } while (numConsecutives < n - 1); 
    return numTosses;
}

那么你只需要调用:

int tosses = numTossesUntilNConsecutiveFaces(3);
public class FlipperThree {
     public static void main(String[] args) { 
         System.out.println("Welcome to Flipper!");
         int previousFlip = -1;
         int continuousFlip = 0;
         int totalFlips = 0;
         String[] ht = new String[] {"Heads","Tails"};
         Random r = new Random();
         while (continuousFlip < 3) {
             int r = r.nextInt(ht.length);
             totalFlips++;
             System.out.println(ht[r]);
             if(previousFlip == r){
                 continuousFlip++
             } else {
                 continuousFlip = 1;
                 previousFlip = r;
             }
         }
         System.out.println("BINGO!That only took " + totalFlips +" flips to get 3 in a row!");
     }
 }

只是想展示一个较短的版本。重点是您不需要跟踪正面或反面,您只需要跟踪您相应地翻转了多少相同的数字。关于现实世界性能的非常小的优化,但仍然是一个改进:)