随机硬币翻转输出运行次数

时间:2015-03-20 22:02:58

标签: java arrays if-statement for-loop

我试图在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;
    }
}

4 个答案:

答案 0 :(得分:0)

删除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 :(得分:0)

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

  2. 您使用==来比较字符串。您应该说random == ht[h]

  3. 而不是random.equals(ht[h])
  4. 头部或尾部计数小于3时,您只想保持循环,而两者小于3时,而不是循环。因此,而不是hcount!=3 || tcount!=3 hcount!=3 && tcount!=3

  5.  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!
    

答案 2 :(得分:0)

当您意识到只需要跟踪连续硬币面的数量时,您可以通过更简单的代码实现此目的。它的面貌无关紧要:

/**
 * 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);

答案 3 :(得分:0)

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

只想展示更短的版本。重点是你不需要跟踪头部或尾部,你只需要跟踪你已经连续多少翻了相同的数字。关于现实世界性能的非常小的优化,但仍然是一种改进:)