Monday, 25 July 2016

WAP: a coin flipping game | Java (Random Number Application)

Q:  WAP, You have to design a coin flipping game. In this game, the program flips the coin. If the user gets head, he earns 1 point. If tails shows up, he loses the game. After he loses ask the player if he wants to play again or quit.

Solution:
import java.util.Random;
import java.util.Scanner;
import java.util.*;
public class Coin
{
public static void main(String args[])
{
int score=0,j,i;
String k;
Random r=new Random();
Scanner s=new Scanner(System.in);
do
{ j=r.nextInt(2); if(j==1)
{
System.out.println("Head");
score++;
System.out.println("Your score: " +score);
}
else if(j==0)
{ System.out.println("Tail");
System.out.println("lose");
}
System.out.println("if you want to play");
k=s.nextLine();
}while(k.equalsIgnoreCase("yes"));
System.out.println("Your final score ");
System.out.println(score);
}
}

0 comments:

Post a Comment