Making Coin Toss for Football game
Hello, I am trying to construct a football simulator game, here is the code that I have so far:
Code :
import java.util.Scanner;
import java.util.Random;
public class gamePlay{
public static int Downs = 0;
public static int Yards = 0;
public static String Coin = null;
public static String Choice = null;
public static String Toss = null;
public static String heads;
public static String tails;
public static String Heads;
public static String Tails;
public static String Yes;
public static String No;
public static void main (String[]args){
Scanner keyboard = new Scanner(System.in);
System.out.println("Welcome to todays game, it's time for the coin toss.");
System.out.println("Please Pick Heads Or Tails By typing you answer.");
String HT = keyboard.nextLine();
switch(HT)
{
case "Heads":
case "heads":
Choice = heads;
//TODO Make Answer Random
coinToss();
break;
case "Tails":
case "tails":
Choice = tails;
//TODO Make Answer Random
coinToss();
break;
}
//TODO Test The Above
if(Coin == Heads && (Choice == heads))
{
System.out.println("You have won the coin toss!");
Toss = Yes;
}else if (Coin == Heads && (Choice == tails)){
System.out.println("You have lost the coin toss...");
Toss = No;
}
if(Coin == Tails && (Choice == tails))
{
System.out.println("You have won the coin toss!");
Toss = Yes;
}else if (Coin == Tails && (Choice == heads)){
System.out.println("You have lost the coin toss...");
Toss = No;
}
if(Toss == Yes)
{
//Insert Something here
}else if(Toss == No)
{
//Insert Something here
}
}
public static void coinToss()
{
Random coin = new Random();
int coinRand = coin.nextInt(2);
if (coinRand == 0)
{
System.out.println("The Result was Heads....");
Coin = Heads;
}else{
System.out.println("The Result was Tails....");
Coin = Tails;
}
}
}
My issue is when I run it, it prints "You have won the coin toss!" twice, no matter what the outcome is, could you explain to me what I am doing wrong?
Re: Making Coin Toss for Football game
This is a shot in the dark, but I would have used the toss = Math.random(). Then if between 0 and .5 would be equal to heads or tails. Then .6 to 1.0 will be
equal to the other.
I would then have a variable call.
Code :
if(call == toss)
{
System.out.println("you won the toss");
}
else
{
System.out.println("You lost the toss");
}
you are printing that out twice because any expression that evaluates to false will print that line, and in both those cases it does.
Re: Making Coin Toss for Football game
Ok, how would I make it only have 1 false and 1 true statement? Would it just be a single if-else statement instead?
Re: Making Coin Toss for Football game
I would try something like this:
Code :
public static String CoinToss(String toss)
{
double call;
double toss2;
toss2 = Double.parseDouble(toss);
String output;
call = Math.random();
if(call == toss)
{
output = "You won the coin toss";
}
else
{
output = "You lost the coin toss";
}
return output;
}
Re: Making Coin Toss for Football game
it should be (call == toss2) sorry
Re: Making Coin Toss for Football game
Your message prints twice because you have (as you frame it) 4 separate outcomes (choose heads / coin heads, choose heads / coin tails, choose tails / coin heads, choose tails / coin tails), and you have *two* if-else statements like
Code :
if (1)
{}
else if (2)
{}
if (3)
{}
else if (4)
{}
where you should have
Code :
if (1)
else if (2)
else if (3)
else if (4)
so that only one condition matches.
That would stop your message appearing twice, but there's another fault in your code: you're assigning
but 'heads' is a String reference that has never been assigned - it is null, as is 'tails'