Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (293)
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  
  Random.nextInt() always returning same number? [Solved]  (Read 468 times)
0 Members and 1 Guest are viewing this topic.
Offline cubemaster21

Senior Member


Medals: 4
Projects: 1



« Posted 2013-01-06 21:34:33 »

For some reason, whenever i call the statement "random.nextInt(5)", it always returns 4. But only in two of the methods that i have, otherwise, it works correctly. The two methods that don't work are as follows.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
   public String getRandomDeathMessage(){
     
      int c = random.nextInt(5);
      String ret = "dead";
      switch(c){
      case 0:ret = "You got dead.";
      case 1:ret = "You done died, son.";
      case 2:ret = "How's the dying going?";
      case 3:ret = "I admit, you should've made that.";
      case 4:ret = "Nice one, slab.";
      }
      return ret;
   }

The second method is essentially a clone of this one, only a different variable.
It always returns "Nice one, slab."
Does anyone know why, or what i could do to fix it?

Check out my game, Viking Supermarket Smash
http://www.java-gaming.org/topics/iconified/28984/view.html
Offline matheus23

JGO Wizard


Medals: 72
Projects: 3


You think about my Avatar right now!


« Reply #1 - Posted 2013-01-06 21:37:56 »

Ough... bad mistake Sad

Common mistake with newcomers, and common mistake with odies as well. You simply often forget it...

Every case needs a
break;
if you don't want the other cases to "execute".
(or a return)

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline cubemaster21

Senior Member


Medals: 4
Projects: 1



« Reply #2 - Posted 2013-01-06 21:40:03 »

yeah, i noticed that immediately after i posted it... I've made this mistake before...
I tried to delete the post, but it wouldn't let me.

Check out my game, Viking Supermarket Smash
http://www.java-gaming.org/topics/iconified/28984/view.html
Games published by our own members! Check 'em out!
Play the free demo of Revenge of the Titans!
Offline matheus23

JGO Wizard


Medals: 72
Projects: 3


You think about my Avatar right now!


« Reply #3 - Posted 2013-01-06 21:47:23 »

I know you already know the answer, but the question exists anyways:
So why the f*ck is that?
Simply said, a common quote: "It's not a bug. It's a feature!".

Let's write a method which returns the number of days a month has. Usually you'd do this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
public static int getDaysOfMonth(String month) {
   // Using a String-switch from Java 7 here.
  // If that wouldn't be available, you'd usually use
  // "public static final *MONTH*"'s.
  switch (month) {
   case "January": return 31;
   case "February": return whatever();
   case "March": return 31;
   case "April": return 30;
   case "May": return 31;
   case "June": return 30;
   case "July": return 31;
   case "August": return 30;
   case "September": return 31;
   case "November": return 30;
   case "December": return 31;
   }
}


You can do this much slicker with the multi-case:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
public static int getDaysOfMonth(String month) {
   switch (month) {
   case "January": case "March": case "May": case "July": case "September": case "December":
      return 31;
   case "April": case "June": case "August": case "November":
      return 30;
   case "February":
      return mightyMagic();
   }
}


So that's why it's like it is Smiley
I know you already solved it, but I hope this makes it clearer anyways Smiley

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline sproingie
« Reply #4 - Posted 2013-01-06 22:10:04 »

In advanced cases (har har) you can also partly execute one case, then fall through to the next one.  I really recommend not doing that sort of thing though; there's almost always better ways to express it.  

The main reason Java's case statement behaves the way it does because that's how C did it.  In C's case, case labels are very bare syntax sugar for a goto-style unconditional jump, which leads to all kinds of happy fun abuse like Duff's Device (FYI, it doesn't work in Java, and it's obsolete for C).  But at least Tom Duff also gave us half of Porter-Duff, so he wasn't completely evil Smiley
Online Roquen

JGO Ninja


Medals: 66



« Reply #5 - Posted 2013-01-06 22:45:24 »

Think: State-machines.
Offline Best Username Ever

Junior Member





« Reply #6 - Posted 2013-01-06 23:06:45 »

yeah, i noticed that immediately after i posted it... I've made this mistake before...
I tried to delete the post, but it wouldn't let me.

Edit the first post and add [Solved] to the beginning of the title instead.
Pages: [1]
  ignore  |  Print  
 
 

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Browse for soundtracks for your game!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (83 views)
2013-05-17 21:29:12

alaslipknot (91 views)
2013-05-16 21:24:48

gouessej (122 views)
2013-05-16 00:53:38

gouessej (114 views)
2013-05-16 00:17:58

theagentd (126 views)
2013-05-15 15:01:13

theagentd (113 views)
2013-05-15 15:00:54

StreetDoggy (158 views)
2013-05-14 15:56:26

kutucuk (180 views)
2013-05-12 17:10:36

kutucuk (180 views)
2013-05-12 15:36:09

UnluckyDevil (187 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.119 seconds with 21 queries.