Semester One Final

Code

    /// Name: Lauren Baird
    /// Period: 7
    /// Program Name: Semester One Final
    /// File Name: SOF.java
    /// Date Finished: 1/21/2016
    
    import java.util.Random;
    import java.util.Scanner;
    
    public class SOF
    {
    	public static void main( String[] args )
    	{
    		Scanner keyboard = new Scanner(System.in);
    		Random rng = new Random();
    
            System.out.println();
            System.out.println( "Hello there!" );
            
            System.out.print( "How many times would you like to flip this coin? " );
            int times = keyboard.nextInt();
            
            while ( times <= 0 || times > 2100000000 )
            {
                // I used a while loop here to make sure the user doesn't enter another invalid number
                
                System.out.println();
                System.out.println( "That number is invalid. Please try again" );
                
                System.out.print( "How many times would you like to flip this coin? " );
                times = keyboard.nextInt();
            }
            
            int numberOfHeads = 0;
            int numberOfTails = 0;
            int total = 0;
            
            for ( int n = 1 ; n <= times ; n = n+1 )
    		{
                // I used a for loop so my program looked cleaner and more organized
                // In my opinion, this is the easiest method to use when counting
                
    			int flip = rng.nextInt(2);
    			String coin;
                // The random number simulator will either roll a 1 or a 2
    
    			if ( flip == 1 )
                {
                    numberOfHeads = numberOfHeads + 1;
                    // If it rolls a one, one is added to the number of heads flipped
                }
                
                else
                {
                    numberOfTails = numberOfTails + 1;
                    // If the roll is not a one, one will be added to the number of tails flipped
                }
    		}
            
            total = numberOfHeads + numberOfTails;
            // I added this to make sure it was flipping the amount of times it was supposed to
            
            System.out.println();
            System.out.println( "The number of Heads = " + numberOfHeads );
            System.out.println( "The number of Tails = " + numberOfTails );
            System.out.println( "The total number of coins flipped is " + total );
            
            double probabilityOfHeads = (double)numberOfHeads / times * 100;
            double probabilityOfTails = (double)numberOfTails / times * 100;
            // This is just to find the probability of getting Heads or Tails
            
            System.out.println();
            System.out.println( "The probability of rolling Heads is " + probabilityOfHeads + "%" );
            System.out.println();
            System.out.println( "The probability of rolling Tails is " + probabilityOfTails + "%" );
            System.out.println();
            
            if ( times%2 == 0 )
            {
                int needed = times / 2;
                
                System.out.println( "In order to get to as close to a 50% chance for heads or tails as possible, " );
                System.out.println( "you would need to roll " + needed + " heads and " + needed + " tails" );
            }
            
            else 
            {
                int Tailsneeded = (times - 1)/ 2 + 1;
                int Headsneeded = (times - 1)/ 2;
                
                int Tailsneeded2 = (times - 1)/ 2;
                int Headsneeded2 = (times - 1)/ 2 + 1;
                
                System.out.println( "In order to get to as close to a 50% chance for heads or tails as possible, " );
                System.out.println( "you would need to roll " + Headsneeded + " heads and " + Tailsneeded + " tails" );
                System.out.println( "or" );
                System.out.println( "you would need to roll " + Headsneeded2 + " heads and " + Tailsneeded2 + " tails" );
            }
            
            // I made sure to check if the number the user put in was even before dividing
            // If I didn't do this and the user put in an odd number, the numbers would not be whole numbers or not add up
            
            System.out.println();
        }
    }
      
    

Picture of the output

SOF SOF