Code prob, this is setting all values equal when it shouldnt!

hey guys, just didnt know what the problem was to this… ill start off by giving you my code for the prob


		if(highscores[0] < score)
			highscores[0] = score;
		else if(highscores[1] < score)
			highscores[1] = score;
		else if(highscores[2] < score)
			highscores[2] = score;
		else if(highscores[3] < score)
			highscores[3] = score;
		else if(highscores[4] < score)
			highscores[4] = score;

ok so after the if(highscores[0] < score) is triggered all the events after it are also triggered. How can I make it so once one event is triggered that’s the only one that happens?

Create a seperate method and put returns in it


		public void checkHighscore() {
		if (highscores[0] < score) {
			highscores[0] = score;
			return;
		} else if (highscores[1] < score) {
			highscores[1] = score;
			return;
		} else if (highscores[2] < score) {
			highscores[2] = score;
			return;
		} else if (highscores[3] < score) {
			highscores[3] = score;
			return;
		} else if (highscores[4] < score) {
			highscores[4] = score;
			return;
		}
	}

it will the exit out of the method soon as it reaches a solution. or continue to the end

edit: froamtted the code

Thats not working :frowning:
this is the code i used

	public static void addScore(int score) {
		if (highscores[0] < score) {
			highscores[0] = score;
			return;
		} else if (highscores[1] < score) {
			highscores[1] = score;
			return;
		} else if (highscores[2] < score) {
			highscores[2] = score;
			return;
		} else if (highscores[3] < score) {
			highscores[3] = score;
			return;
		} else if (highscores[4] < score) {
			highscores[4] = score;
			return;
		}
	}
/*Breakpoint*/ if(highscores[0] < score) // Put a breakpoint on this line.
                  highscores[0] = score;
                else if(highscores[1] < score)
                  highscores[1] = score;
                else if(highscores[2] < score)
                  highscores[2] = score;
                else if(highscores[3] < score)
                  highscores[3] = score;
                else if(highscores[4] < score)
                  highscores[4] = score;

First: Learn how to use a debugger. Pause the program on that line. Step over each line. Confirm whether each line works the way you expect it to.
Second: Use a loop instead of if-else-if-else-if-else-if-else-if-else-if

It means [icode]score[/icode] is less than all the elements in [icode]highscore[/icode]

Is this the actual code in the method? Are you sure the issue is with these lines? With the if/then/else chain, I don’t see why more than one branch would execute. Is it being called repeatedly? If it is, then that would explain why it would set all of the array values equal to score - one at a time.

Are you two unable to comprehend the ‘else’ statement?

That is the exact code I am using, and returns dont help the problem at all lol

No.* I assumed that because ALL of the if’s are triggered, it means none of them were true. (Apparently, I was mistaken. I thought he meant the statement, not the body that was triggered ::))

Most likely suggestion so far.

(By the way, once you have the new highscore, you should be bumping the lesser scores down instead of replacing the one)

(*Edit: For anyone who didn’t see, I read Riven’s question the other way around and said yes. ;D)

My thoughts were, if for some reason what ever is causing it to still execute, he could just make sure 200% that it wont be executed by making it hit a return statement.

It seems your assumption was that the compiled Java code itself misbehaved, which would mean there was a bug in the JVM, so you wanted it to misbehave a tad less, but making doubly sure the control-flow was enforced… Why stop there, and make it 300% by suggesting yet another control-flow keyword?

guys im really confused on how to fix my problem…

Putting in returns at least made it easier to determine the problem.

If it didn’t work even with the return statements, it means either as said above: It’s being called multiple times, or there is a bug in the JVM.

Find where it’s being called from and check it’s beig called once. Or run a test with if-else statements where you KNOW it isn’t being called multiple times.

Reply with your findings and we’ll see what happens

The addScore method is called two times for if the game is over or if the game is quit (paused in android land)

pop a


System.out.println("adding score");

to make sure its not being called more then twice. You may of accidently put it outside of a if statement.


if(a == b)
{
	while(true)
	{
		int i = 0;
		if(a ==b)
			i = 1;
		switch(i)
		{
			case 1:
			c = true;
			return;
		}
	}
}
else
{
	while(true)
	{
		if(a==b)
			i = 1;
		else
			i=2;
		switch(i)
		{
			case 2:
			c = false;
			return;
		}
	}
}

you have just made me more confused on what I am doing

Hero is replying to Riven’s statement that Hero isn’t realising is sarcasm/mocking.

Did you try putting a


System.out.println("adding score");

at the beginning of the adding score method?

As said before, the bug is in the code that is calling your code snippet. To prove it to you, try running this code:


		System.out.println("before");
		if(highscores[0] < score) {
			System.out.println("block 1");
			highscores[0] = score;
		}
		else if(highscores[1] < score) {
			System.out.println("block 2");
			highscores[1] = score;
		}
		else if(highscores[2] < score) {
			System.out.println("block 3");
			highscores[2] = score;
		}
		else if(highscores[3] < score) {
			System.out.println("block 3");
			highscores[3] = score;
		}
		else if(highscores[4] < score) {
			System.out.println("block 4");
			highscores[4] = score;
		}
		System.out.println("after");

And you’ll see at most one of these if-blocks is executed.

So the bug is not in this code, but somewhere else.

ahhhhh looks like the problem is found…

http://pastebin.com/nyYgCKZm

ps im doing this on android. I know that it would be hard for you to know how to fix this without seeing the code, but any suggestions on how to figure this one out?