• Post Reply Bookmark Topic Watch Topic
  • New Topic

About Math.random  RSS feed

 
F Lucas
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, this is my first time here.
I was making a Java program as an exercise, which is like a little Vehicle Simulator. And there is an method where,
1. It generates a random number with the name "Tempinitial"
2. It checks if "Tempinitial" is the same as "Initial"
3. If it is, it regenerates the number with the exception of the number assigned to "Initial"
4. Else, it will just set the number from "Tempinitial" to "Initial"
5. When you go check the Color, it will set the "PaintColor" to a color (a string that represents a color, like "Red") depending on which number it is.
But I'm having problems in step 1, because it always uses the default case even though it can use the case 1.
Please help me. 

the code:



      
       
      
             
   

 
Piet Souris
Master Rancher
Posts: 2044
75
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi F Lucas,

welcome to the ranch! I hope you'll enjoy your stay here.

There are quite a few things that can be said about your code. But for me, the most important thing is that if you are using a 'switch', then you would normally use the 'break' command in your cases.
If you have, for instance:

and if i = 1, then can you guess what the output will be?
So, have a look at Oracle's tutorial about 'switch': 'switch'

The fact that you do not notice anything strange here is that you call the method 'programStart()' from many places. This is a bad construct. But let's talk about that another time.

You get into the default 'Something went wrong' when the variable 'Initial' becomes 6 or larger. Can you find the location in your 'Paint' method where it is possible for that variable to become bigger than 5? Inspect your code carefully!

For now, there are two things that will make your code easier to follow:

in your 'programStart()' method, insrt this line:


and in your 'CheckColors()' method, add the line:
 
Diego Nobrega
Greenhorn
Posts: 3
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I dont know if you are new in the java world.. but try to organize your code according Java Convention..
Sorry beeing **** about this but this is important..
name of attrib, variables and methods start lowercase: static String paintColor for example..
It mess up who is reading your code and couldnt get to put it on eclipse or anything else for test

About the your concern.. i guess the Piet did good answer.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

I don't like Math#random. There are far better ways to get a “random” int. For one thing, Math#random uses a strange method of calculating its result. There is more discussion in this thread, along with my recommendation of a better way to do it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!