[英]Coin Flip Sequence
I am stuck with this project. 我坚持这个项目。 I have to write a coin flip program that generates HTH (121) and I have to count how many tosses would it take to generate that sequence.After that I would need to calculate the average of the sequence.I have the code down below.
我必须编写一个生成HTH(121)的硬币翻转程序,我必须计算生成该序列需要多少次抛出。之后我需要计算序列的平均值。我将代码放在下面。 For example: If I get the output of
例如:如果我得到的输出
1112222222122122122211222121
28
11111122121
11
11121
5
Then my average should be (28+11+5)/3=14.66667. 然后我的平均值应为(28 + 11 + 5)/3=14.66667。 However, i'm stuck coding the average.
但是,我对平均值进行了编码。 I have the for loop set up but I'm unsure where or how to put the code for the average.
我有for循环设置,但我不确定在何处或如何将代码放入平均值。
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 statment witout the compiler saying, "count cannot be resolve to 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;
}
}
Well first you need to track the total flips across all loops: 首先,您需要跟踪所有循环中的总翻转:
int totalFlips = 0;
Then you need to increment this with each flip: 然后你需要在每次翻转时递增:
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 is full 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.
}
Although musefan's solution is entirely correct it feels a bit weird to me to count both the "count" in the run and the "totalFlips" during the run. 虽然musefan的解决方案是完全正确的,但在运行期间计算运行中的“计数”和“totalFlips”时,我觉得有点奇怪。 I would just do 1 run and add that run's "count" to the totalFlips afterwards.
我会做一次运行,然后将运行的“计数”添加到totalFlips。 To make it even more obvious I'll also put one run in a separate method.
为了使它更加明显,我还将一次运行放在一个单独的方法中。 (Following code based on musefan's):
(以下代码基于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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.