Skip to content

Instantly share code, notes, and snippets.

@nasko90
Created September 27, 2016 20:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nasko90/244e515242525f214ce0eaf3ca780602 to your computer and use it in GitHub Desktop.
Save nasko90/244e515242525f214ce0eaf3ca780602 to your computer and use it in GitHub Desktop.
import java.util.Scanner;
public class HeadAndTails {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println(" Write down the results from fliping the coin, separated by coma: 'H' for heads win, 'T' for tails win ");
String result = input.nextLine();
String [] resultArray = result.split(",");
int tWins = 0;
int hWins = 0;
int maxT = 0;
int maxH = 0;
for (int i =0; i <resultArray.length; i++){
if (resultArray[i].equals("T")){
tWins++;
if (maxT<tWins){
maxT=tWins;
}
}
else {
tWins = 0;
}
if (resultArray[i].equals("H")){
hWins++;
if (maxH<hWins){
maxH=hWins;
}
}
else {
hWins =0;
}
}
if (maxH>maxT){
System.out.println("H wins !");
}
else if (maxH==maxT){
System.out.println("Draw !");
}
else {
System.out.println("T wins !");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment