# Coin toss question

Page 1 of 1

## 4 Replies - 1584 Views - Last Post: 17 July 2012 - 10:13 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=286028&amp;s=df4e5791662b8d51db77324f0ee9b148&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 HIgoalie

Reputation: 0
• 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;

{
/**
Create coinToss method
@param This method simulates the tossing of a coin.
*/
public static void coinToss()

{
int toss;
int counter;
String input;

counter = 0;
tails = 0;

input = JOptionPane.showInputDialog("How many times do you want the coin to be tossed? ");
toss = Integer.parseInt(input);
if (toss < 1)
{
}

Random coinToss = new Random();
int random = coinToss.nextInt(2);

/**
@param 1 = tails
*/
while (counter < toss)
{

if (random > 0.5)
{
}

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

• Bloodborne

Reputation: 522
• Posts: 2,102
• 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.

### #3 kassanova

Reputation: 0
• 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
}

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
}

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

```

### #4 pbl

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

Reputation: 8378
• Posts: 31,956
• 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

### #5 HIgoalie

Reputation: 0
• 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.