Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (497)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Code prob, this is setting all values equal when it shouldnt!  (Read 2421 times)
0 Members and 1 Guest are viewing this topic.
Offline wreed12345

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Posted 2012-12-08 00:39:27 »

hey guys, just didnt know what the problem was to this... ill start off by giving you my code for the prob
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
      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?

Offline Phased
« Reply #1 - Posted 2012-12-08 00:43:22 »

Create a seperate method and put returns in it

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
      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
Offline wreed12345

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #2 - Posted 2012-12-08 00:47:49 »

Thats not working Sad
this is the code i used
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
   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;
      }
   }

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Best Username Ever

Junior Member





« Reply #3 - Posted 2012-12-08 00:52:41 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
/*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
Offline HeroesGraveDev

JGO Kernel


Medals: 253
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #4 - Posted 2012-12-08 00:52:46 »

It means
score
is less than all the elements in
highscore

Offline actual

JGO Coder


Medals: 23



« Reply #5 - Posted 2012-12-08 00:58:03 »

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.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 799
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #6 - Posted 2012-12-08 01:02:53 »

It means
score
is less than all the elements in
highscore

Create a seperate method and put returns in it
Are you two unable to comprehend the 'else' statement?

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline wreed12345

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #7 - Posted 2012-12-08 01:04:23 »

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

Offline HeroesGraveDev

JGO Kernel


Medals: 253
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #8 - Posted 2012-12-08 01:31:30 »


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  Roll Eyes)

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.

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. Grin)

Offline Phased
« Reply #9 - Posted 2012-12-08 01:36:27 »

Are you two unable to comprehend the 'else' statement?

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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 799
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #10 - Posted 2012-12-08 01:45:05 »

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?

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline wreed12345

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #11 - Posted 2012-12-08 01:49:37 »

guys im really confused on how to fix my problem...

Offline HeroesGraveDev

JGO Kernel


Medals: 253
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #12 - Posted 2012-12-08 01:50:06 »

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.

guys im really confused on how to fix my problem...

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

Offline wreed12345

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #13 - Posted 2012-12-08 01:54:24 »

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

Offline Phased
« Reply #14 - Posted 2012-12-08 01:55:50 »

pop a
1  
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.
Offline HeroesGraveDev

JGO Kernel


Medals: 253
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #15 - Posted 2012-12-08 01:57:57 »

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?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
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;
      }
   }
}

Offline wreed12345

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #16 - Posted 2012-12-08 02:07:09 »

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

Offline Phased
« Reply #17 - Posted 2012-12-08 02:09:30 »

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

Did you try putting a
1  
System.out.println("adding score");


at the beginning of the adding score method?
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 799
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #18 - Posted 2012-12-08 02:10:28 »

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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
      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.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline wreed12345

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #19 - Posted 2012-12-08 02:27:36 »

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?

Offline HeroesGraveDev

JGO Kernel


Medals: 253
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #20 - Posted 2012-12-08 02:28:09 »

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

Did you try putting a
1  
System.out.println("adding score");


at the beginning of the adding score method?

HA!

I did realise the sarcasm/mocking/whatever-you-want-to-call-it! You were the one who didn't notice mine! LOL  Grin

But yeah, good suggestion. @OP: Do what he and Riven said

Offline Phased
« Reply #21 - Posted 2012-12-08 02:30:01 »

from that, we can see, your code is being called multiple times instead of at the end of the game or on exit (unless you didnt specify that you finished like 10 goes at your game.

Check where you call the addScore(score) method, that is where its being called multiple times.
Offline wreed12345

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #22 - Posted 2012-12-08 02:47:28 »

Thanks guys for all the help! the problem was just where i was calling the method. after 5 hours and my eyes hurting problem solved

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

BurntPizza (21 views)
2014-09-19 03:14:18

Dwinin (35 views)
2014-09-12 09:08:26

Norakomi (62 views)
2014-09-10 13:57:51

TehJavaDev (87 views)
2014-09-10 06:39:09

Tekkerue (42 views)
2014-09-09 02:24:56

mitcheeb (65 views)
2014-09-08 06:06:29

BurntPizza (47 views)
2014-09-07 01:13:42

Longarmx (35 views)
2014-09-07 01:12:14

Longarmx (40 views)
2014-09-07 01:11:22

Longarmx (36 views)
2014-09-07 01:10:19
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!