尝试这个:
public class CoinFlips{
public static void main(String[] args){
private int headsCount, tailsCount, countTails, countHeads, maxHeads, maxTails, lastFlip = 0;
private static final int HEADS = 1;
private static final int TAILS = 2;
for (int i=0 ; i<=200; i++){
double x= Math.random();
if (x<0.5){
if (lastFlip == TAILS) {
tailsCount++;
if (tailsCount > maxTails) {
maxTails = tailsCount;
}
} else {
tailsCount = 1;
}
lastFlip = TAILS;
countTails++;
}
else {
if (lastFlip == HEADS) {
headsCount++;
if (headsCount > maxHeads) {
maxHeads = headsCount;
}
} else {
headsCount = 1;
}
countHeads++;
}
}
StringBuilder sb = new StringBuilder();
sb.append("There were ").append(countHeads)
.append(" heads flipped with the maximum sequential flips of ")
.append(maxHeads).append(".\n")
.append("There were ").append(countTails)
.append(" tails flipped with the maximum sequential flips of ")
.append(maxTails).append(".\n");
System.out.print(sb.toString());
}
}
`