vendredi 20 mars 2015

Random Coin Flipper output number of times run

I am attempting to write a program in java that flips an imaginary coin and outputs the flips and then when a certain side has been flipped 3 times, it stops and tells you the number of times it flipped. My program doesn't seem to be working


My code is below:



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;
}
}

Aucun commentaire:

Enregistrer un commentaire