I was assigned to create a method that simulates flipping a coin. I think my code here is pretty solid, only I can't get it to show a series of results after n number of flips. So instead of showing HTHHHTTTHTT, I'm only getting H or T.
public static void main(String[] args) {
System.out.println((flipCoin(2, 10)));
}
public static String flipCoin(int chance, int numFlips) {
String result = "";
Random rando = new Random();
chance = rando.nextInt();
for (int i = 0; i < numFlips; i++) {
if (chance % 2 == 0) {
result = "H";
}
else {
result = "T";
}
numFlips++;
}
return result;
}
Aucun commentaire:
Enregistrer un commentaire