4 Replies - 985 Views - Last Post: 17 July 2012 - 10:13 PM Rate Topic: -----

#1 HIgoalie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 16-July 12

Coin toss question

Posted 16 July 2012 - 11:45 PM

Here's what I have so far

import java.util.Random;			
import javax.swing.JOptionPane;

public class headsTails 
{
	/**
	 	Create coinToss method
	 	@param This method simulates the tossing of a coin.
	*/
	public static void coinToss()
	
	{
		int toss;			
		int counter;	  	
		int heads, tails;	
		String input;		

		counter = 0;	
		heads = 0;		
		tails = 0;		
		
		input = JOptionPane.showInputDialog("How many times do you want the coin to be tossed? ");	
		toss = Integer.parseInt(input);		
		if (toss < 1)
		{
			System.out.println("Please enter a positive number.");
		}
		
		Random coinToss = new Random();
		int random = coinToss.nextInt(2);
	
		/**
		 	Heads or tails loop
		 	@param 0 = heads
		 	@param 1 = tails
		*/	
		while (counter < toss)
		{
		
			if (random > 0.5) 
			{
				heads = heads + 1;
				System.out.println("It's heads");
			}
		
			else 
			{
				tails = tails + 1;
				System.out.println("It's tails");
			}
			
			counter++;
			random = coinToss.nextInt(2);
		}
	}
	
	/**
	 	Recall coinToss method
	 	@param The coinToss method is called and generates random numbers in the range of 0 through 1.
	 	@param If the random number is 0, the method should display "heads".
	 	@param If the random number is 1, the method should display "tails".
	*/
	public static void main (String[] args)
	{
		coinToss();
	}
}


I'm having a problem with the program accepting negative numbers, which it's not suppose to. This is the only problem I'm having. I've tried many different loops and been working on this for a while, just need a little help keeping it from accepting negative numbers.

Is This A Good Question/Topic? 0
  • +

Replies To: Coin toss question

#2 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Coin toss question

Posted 17 July 2012 - 12:21 AM

Just do a do-while loop. Keep asking for input until the input is acceptable. How to find out if it is negative? Why, ask if it is < 0. :)
Was This Post Helpful? 1
  • +
  • -

#3 kassanova  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 16-July 12

Re: Coin toss question

Posted 17 July 2012 - 01:16 AM

i don quiet get ur question ,If the input is negative u just ask the user to enter positive number what you have their is good moreover you can't accept negative numbers in the input because you can't toss the coin negative number of times wht u have their is correct but a little improvement would be .

while (counter < toss)
		{
		
			if (random == 0) 
			{
				System.out.println(random);// you can get rid of this if you don want this bit
				System.out.println("It's heads");
			}
		
			else 
			{
				System.out.println(random);you can get rid of this if you don want this bit
				System.out.println("It's tails");
			}
			
			counter++;
			random = coinToss.nextInt(2);
		}




ahhh i messed up a little bit heres that bit again
while (counter < toss)
		{
		
			if (random == 0) 
			{
				System.out.println(random);// get rid of this this was 4r me to check
				System.out.println("It's heads");
			}
		
			else 
			{
				System.out.println(random);// get rid of this this was 4r me to check
				System.out.println("It's tails");
			}
			
			counter++;
			random = coinToss.nextInt(2);
		}


Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Coin toss question

Posted 17 July 2012 - 11:10 AM

int random = coinToss.nextInt(2);

as the value of random will be 0 or 1 you have to test for these values tio determine if tail or head
Was This Post Helpful? 0
  • +
  • -

#5 HIgoalie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 16-July 12

Re: Coin toss question

Posted 17 July 2012 - 10:13 PM

thank you all who commented. Everyone's input was quite helpful and I think I can get it from here.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1