//Hello(apologize for any formatting errors)
//I am trying to finish this program that is supposed to act as a club bouncer. Only letting a max amount of people (125) in at one time. Naturally people leave throughout the night, so I was trying to //implement that with the random variable, but it keeps producing negative values. Any help would be greatly appreciated.

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

       public class Clubmanger
    {
       public static void main(String[] args)
       {
          int counter = 0;
            Random leaving=new Random();
        do
        {
            int addperson=Integer.parseInt(JOptionPane.showInputDialog("Welcome to Club G, how many people in your party?"));
            if(addperson+counter>125)
            {
                JOptionPane.showMessageDialog(null,"There are too many people inside the club to allow you entry, please try again later");
                System.exit(0);
            }
            else
            {
                counter=+addperson;
                JOptionPane.showMessageDialog(null,"There are currently: "+counter+" people inside.");
            }
            int personleaving=leaving.nextInt(counter)+1;
            counter=-personleaving;
            JOptionPane.showMessageDialog(null,"There are:"+counter+"inside the club.");

        }while(counter<=124);    
    }

    }

Recommended Answers

All 3 Replies

that is because tour random variable has values greater than your counter, try to limit your randomization to the counter

but I want to limit it to only the values that the counter has. I don't want it to generate per say 80 if there are only 30 people inside

No your random variable is giving correct value.The error is because of line 24

  counter=-personleaving;//you are makin the value negative here by taking negative of personleaving

I think so you want to subtract personleaving from counter.It should be:-

 counter-=personleaving; //subtract personleaving from counter.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.