David de Groot David de Groot - 5 months ago 11
Java Question

int nog getting incremented after my if loop finishes?

Currently I'm learning java by myself and I have a long ways to come, but I've recently started a thing I wanted to make, but there's something wrong with it.
I've copied a part of my code below which is inside the main. The variable names are dutch, but those can be ignored mostly.

Basically what I want it to do is increase the

somnummer
by 1 after
som1
has been completed correctly, but currently that's not happening (checking it with the print at the end, is always 1).

Is it wrong of me to assume that it first completes the if function and everything in it and then move on to the
somnummer++
?


My code might seem like a bit of a mess, but I'm still very new to it. If possible I'd like the solution to be without too much new stuff, I'll reach that part sooner or later in my course ;)

Thanks in advance!

Scanner input = new Scanner(System.in);

int somnummer = 1;
if(somnummer==1){
System.out.println("");
System.out.println(som1);

do{
antwoord=input.nextInt();
if(antwoord!=5){
System.out.println(fout);
}
}

while(antwoord!=5);
System.out.println(goed);
antwoord=0;
}

somnummer=somnummer++;
System.out.println(somnummer);

Answer

You must use

somnummer++;

Instead of

somnummer = somnummer++;

To increment somnummer