Skip to content

Instantly share code, notes, and snippets.

@imryan
Created November 20, 2013 16:18
Show Gist options
  • Save imryan/7566003 to your computer and use it in GitHub Desktop.
Save imryan/7566003 to your computer and use it in GitHub Desktop.
Coinflip program that was extremely necessary to complete.
import java.util.*;
public class Coinflip {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int sideNo = 0;
String side = "";
String landedSide = "";
System.out.println("Choose side (1 = Heads | 2 = Tails): \n");
sideNo = sc.nextInt();
if (sideNo == 1) {
side = "Heads";
}
else if (sideNo == 2) {
side = "Tails";
} else {
main(args);
}
landedSide = flipCoin();
System.out.println("Landed on " + landedSide + "!\n");
if (landedSide == side) {
System.out.println("You won!\n");
} else {
System.out.println("You lost!\n");
System.exit(0);
}
}
public static String flipCoin() {
Random random = new Random();
int side = random.nextInt(3);
if (side == 1) {
return "Heads";
} else {
return "Tails";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment