Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Making Coin Toss for Football game

  1. #1
    Member
    Join Date
    Sep 2011
    Posts
    40
    My Mood
    Inspired
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default 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:

    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?


  2. #2
    Member
    Join Date
    Aug 2011
    Posts
    55
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Default 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.

    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.

  3. #3
    Member
    Join Date
    Sep 2011
    Posts
    40
    My Mood
    Inspired
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default 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?

  4. #4
    Member
    Join Date
    Aug 2011
    Posts
    55
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Default Re: Making Coin Toss for Football game

    I would try something like this:

     
    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;
    }

  5. #5
    Member
    Join Date
    Aug 2011
    Posts
    55
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Default Re: Making Coin Toss for Football game

    it should be (call == toss2) sorry

  6. #6
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default 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
    if (1)
    {}
    else if (2)
    {}
    if (3)
    {}
    else if (4)
    {}
    where you should have
    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
    		Choice = heads;
    but 'heads' is a String reference that has never been assigned - it is null, as is 'tails'

Similar Threads

  1. [SOLVED] Java Noob: Coin Toss Simulator (no GUI)
    By bgroenks96 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 4th, 2011, 10:28 PM
  2. Making a Craps game.
    By SOK in forum Object Oriented Programming
    Replies: 1
    Last Post: March 6th, 2010, 08:23 PM
  3. Question about my coin toss program
    By CheekySpoon in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 9th, 2010, 04:14 AM
  4. Theory behind 2d Game making?
    By DarrenReeder in forum Java Theory & Questions
    Replies: 3
    Last Post: January 28th, 2010, 02:54 AM
  5. Problem while programming a simple game of Car moving on a road
    By rojroj in forum Java Theory & Questions
    Replies: 3
    Last Post: April 2nd, 2009, 10:24 AM