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.