Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (603)
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 1153 times)
0 Members and 1 Guest are viewing this topic.
Offline cubemaster21
« Posted 2013-01-06 20: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
Online matheus23

JGO Kernel


Medals: 114
Projects: 3


You think about my Avatar right now!


« Reply #1 - Posted 2013-01-06 20: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
« Reply #2 - Posted 2013-01-06 20: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!
Legends of Yore - The Casual Retro Roguelike
Online matheus23

JGO Kernel


Medals: 114
Projects: 3


You think about my Avatar right now!


« Reply #3 - Posted 2013-01-06 20: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

JGO Kernel


Medals: 202



« Reply #4 - Posted 2013-01-06 21: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
Offline Roquen
« Reply #5 - Posted 2013-01-06 21:45:24 »

Think: State-machines.
Offline Best Username Ever

Junior Devvie





« Reply #6 - Posted 2013-01-06 22: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  
 
 
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.

rwatson462 (37 views)
2014-12-15 09:26:44

Mr.CodeIt (31 views)
2014-12-14 19:50:38

BurntPizza (62 views)
2014-12-09 22:41:13

BurntPizza (99 views)
2014-12-08 04:46:31

JscottyBieshaar (60 views)
2014-12-05 12:39:02

SHC (74 views)
2014-12-03 16:27:13

CopyableCougar4 (77 views)
2014-11-29 21:32:03

toopeicgaming1999 (138 views)
2014-11-26 15:22:04

toopeicgaming1999 (127 views)
2014-11-26 15:20:36

toopeicgaming1999 (38 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!