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

2020/1/30

Find out if an integer has been defined

Starlord_20 Starlord_20

2020/1/30

#
Hello, I have one little problem, in my scenario i want to find out somehow whether an integer has already been defined. Is that possible?
danpost danpost

2020/1/31

#
Starlord_20 wrote...
Hello, I have one little problem, in my scenario i want to find out somehow whether an integer has already been defined. Is that possible?
Need context and name of integer. Show all relevant/related codes.
Starlord_20 Starlord_20

2020/1/31

#
boolean pasch = false;
int geld,z1,z2,z3;

    public void act() {

        if(geld != 0) {
            z1 = (int) (6*Math.random()+1);z2 = (int) (6*Math.random()+1);z3 = (int) (6*Math.random()+1);
            getWorld().showText(""+z1+"   "+z2+"   "+z3,getWorld().getWidth()/2,3);
            getWorld().showText("Zahlen: ",getWorld().getWidth()/2,1); geld -= 2;
            getWorld().showText("Geld: "+geld,getWorld().getWidth()/2,5);
            if((z1 == z2) ||(z2 == z3) ||(z1 == z3)) geld += 5;
            if((z1 == z2) && (z2 == z3)) {
                getWorld().showText("PASCH!",getWorld().getWidth()/2,getWorld().getHeight()/2);stop();
            }
        } else {
            getWorld().showText("Game OVER!",getWorld().getWidth()/2,getWorld().getHeight()/2);stop();
        }

    }
I want to say that if int geld hasn`t been defined, he asks for an int input with :
String a = JOptionPane.showInputDialog("please enter a valid int");
int i = Integer.parseInt(a);
danpost danpost

2020/1/31

#
No worries. Your line 2 declares it as the primitive type, int, which automatically, by default, is assigned a value of zero. Side note: had you declared it as type Integer (which is an object reference type), only then would its default value be null,.
You need to login to post a reply.