This site requires JavaScript, please enable it in your browser!
Greenfoot back
heitorpereira
heitorpereira wrote ...

2016/3/10

How to add a certain number of objects?

heitorpereira heitorpereira

2016/3/10

#
The user must input an int, and then the game have to put that number of objects. This is what i think so far, but after the prompt, nothing happens.
public void act() {
        JOptionPane.showInputDialog("Digite o numero de personagens: ");
        int numeroAtores = scanner.nextInt();

        for (int i = 0; i <= numeroAtores; i++) {
            addObject(new camelo(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
            addObject(new sapo(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
        }
danpost danpost

2016/3/10

#
I think you are confused as far as what input method you want to use. Either use a Scanner method or a JOptionPane method. The JOptionPane method 'showInputDialog' returns the value input as a String object and must be captured:
String stringValue = JOptionPane.showInputDialog("Digite o numero de personagens: ");
This string value must then be converted to a number type value to be used as the limit in the 'for' parameter.
You need to login to post a reply.