Tossing coin, rolling dice and choosing a card in Java
Hi there, I have come up with this program which will help you make a decision, whether it’s through tossing a coin, or maybe rolling a dice, or through choosing a playable card through a deck of fifty two cards. This program is the solution to all three of them.
What the program does is ask, whether the user would prefer a coin, or card, or a dice. Once the user has inputted their desired choice, the program runs, and prints out a valid output.
In the making of this program, I have used three more methods apart from main, to provide the result.
Using methods offer flexibility and re-usability to our program.
Rolling dice, tossing a coin and choosing a card Java program
import java.util.*;
class decide
{ private static void droll()
{
double randomDouble=Math.random();
randomDouble=randomDouble*(6)+1;
int randomInt=(int) randomDouble;
System.out.println("The Dice gave out: " + randomInt);
}
private static void toss()
{ double randomDouble=Math.random();
randomDouble=randomDouble*(2)+1;
int randomInt=(int) randomDouble;
if(randomInt==1)
System.out.println("The Coin gave out Head");
else
System.out.println("The Coin gave out Tails");
}
private static void cards()
{
double randomDouble=Math.random();
randomDouble=randomDouble*(13)+1;
int randomInt=(int) randomDouble;
System.out.println("Your card is :");
if(randomInt==11)
System.out.print("Jack of ");
else if(randomInt==12)
System.out.print("King of ");
else if(randomInt==13)
System.out.print("Queen of ");
else
System.out.print(randomInt+" of ");
double suit=Math.random();
suit=suit*(4)+1;
int suint=(int) suit;
if(suint==1)
System.out.print("Spades");
else if(suint==2)
System.out.print("Clubs");
else if(suint==3)
System.out.print("Hearts");
if(suint==4)
System.out.print("Diamonds");
}
public static void main()
{
String ch;
Scanner sc=new Scanner(System.in);
System.out.println("Enter your choice of: coin/dice/card");
ch=sc.next();
ch=ch.toLowerCase();
switch(ch)
{
case "coin" :
toss();
break;
case "dice" :
droll();
break;
case "card" :
cards();
break;
default :
{
System.out.println("Please provide a valid input");
main();
}
}
}
}
Output :
Enter your choice of: coin/dice/card coin The Coin gave out Head Enter your choice of: coin/dice/card DICE The Dice gave out: 2 Enter your choice of: coin/dice/card Card Your card is : 9 of Hearts Enter your choice of: coin/dice/card NoT Please provide a valid input Enter your choice of: coin/dice/card
Explanation :
Apart from the main function, I have three more methods which are:
- droll(): which is short for a dice roll, it gives out a random number between one to six in the output terminal.
- toss(): which as the name suggests is short for a Coin toss; it gives out either Head or Tail in the output screen.
- cards(): Which again as the name suggests, picks up a playable card from a deck of fifty two cards and displays the result in the output terminal.
How to print a random card from a deck of cards in Java
Guess The Number Game Using Java with Source Code
The main begins by taking a String input from the user and converting it into lowercase to avoid any discrepancy. The next step is issuing a Switch case, which takes in the string and calls the associated function with it. That is for input :
- ‘card’ it calls the cards() method.
- ‘coin’ it calls the toss() method.
- ‘dice’ it calls the droll() method.
and in case the input provided by the user is not valid, It asks the user to provide a valid input and calls the main body again.
Leave a Reply