Coin rotation sequence

I am stuck with this project. I have to write a coin transfer program that generates HTH (121), and I need to calculate how many junk it will take to generate this sequence. After that I will need to calculate the average of the sequence. I have the code below. For example: if I get output

1112222222122122122211222121
28
11111122121
11
11121
5

      

Then my average should be (28 + 11 + 5) / 3 = 14.66667. However, I am stuck with coding the mean. I have a for loop, but I'm not sure where or how to put the code for the middle.

I was thinking of doing something like

int average= count/N;

      

but I don't know how to get each count added to each other and where to put this statute without a compiler: "count cannot be resolved to a variable"

 package HTX_Program;


public class coinSequence {
        public static int coinFlip() {
            MultiDie coin= new MultiDie(2);
            coin.roll();
            int x = coin.getFaceValue();
            return x;
            }

public static void main(String[] args) {
    final int N=3;
    for(int i=0; i<N; i++){
    String sequenceSoFar = "";
    sequenceSoFar += coinFlip();
    sequenceSoFar += coinFlip();
    sequenceSoFar += coinFlip();
    int count = 3;
    if(!sequenceSoFar.equals("121")) {
        while(!(sequenceSoFar.substring(sequenceSoFar.length() - 3).equals("121"))) {
            sequenceSoFar += coinFlip();
            count++;
        }
    }

    System.out.println(sequenceSoFar);
    System.out.println(count);
    }
    int average= count/N;
}
}

      

+3


source to share


2 answers


Well, first you need to track the total number of transitions across all loops:

int totalFlips = 0;

      

Then you need to increase it with every translation:

totalFlips++;

      



Then finally, after the loop, you can calculate the average:

// Note: You have to convert one of the integers to double in order to get correct result.
double average = totalFlips / (double)N;

      

Here's the complete modified function:

public static void main(String[] args) {
    final int N=3;
    int totalFlips = 0; // Track total flips.

    for(int i=0; i<N; i++){
        String sequenceSoFar = "";
        sequenceSoFar += coinFlip();
        sequenceSoFar += coinFlip();
        sequenceSoFar += coinFlip();
        int count = 3;
        totalFlips += 3; // add initial 3 flips to total.
        if(!sequenceSoFar.equals("121")) {
            while(!(sequenceSoFar.substring(sequenceSoFar.length() - 3).equals("121"))) {
                sequenceSoFar += coinFlip();
                count++;
                totalFlips++; // increment total flips.
            }
        }

        System.out.println(sequenceSoFar);
        System.out.println(count);
    }

    double average = totalFlips / (double)N; // Calculate average.
}

      

+2


source


Even though musefan's solution is completely correct, it seems a bit odd to me to count both the "score" in the run and the "totalFlips" during the run. I would just do 1 launch and add that launch "count" to totalFlips afterwards. To make it even more obvious, I also put one launch in a separate method. (The following code is based on musefan's):



public static void main(String[] args) {
    final int N=3;
    int totalFlips = 0; // Track total flips.

    for(int i=0; i<N; i++){
        int count = nbRollsInNewSequence();
        totalFlips += count;
    }

    double average = totalFlips / (double)N; // Calculate average.
}

//rolls a die until the sequence "121" comes up. 
//Prints this count and the sequence and then returns the count.
public static int nbRollsInNewSequence(){
    String sequenceSoFar = "";
    sequenceSoFar += coinFlip();
    sequenceSoFar += coinFlip();
    sequenceSoFar += coinFlip();
    int count = 3;
    if(!sequenceSoFar.equals("121")) {
        while(!(sequenceSoFar.substring(sequenceSoFar.length() - 3).equals("121"))) {
            sequenceSoFar += coinFlip();
            count++;
        }
    }

    System.out.println(sequenceSoFar);
    System.out.println(count);
    return count;
}

      

0


source







All Articles