Created
November 20, 2013 16:18
-
-
Save imryan/7566003 to your computer and use it in GitHub Desktop.
Coinflip program that was extremely necessary to complete.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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