• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why cant I user Math.random?  RSS feed

 
Dwen Hal
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why can't I user Math.random in a class, in order for it to work I must specificity put it in method ? For instance:

 
Dave Tolls
Ranch Foreman
Posts: 3061
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neither of those will compile.
random() returns a double so the compiler complains about incompatible types.

But if numberOne and numberTwo were both doubles then both would be valid.
 
Ganesh Patekar
Bartender
Posts: 726
23
Eclipse IDE Hibernate jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch!
Dwen Hal wrote:
I suspect, did you really compile this? ; is missing then how could It compile successfully?
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
welocome to javaranch Dwen.
it will run ofcourse their must be a problem with otherpart in your code.can you please post your complete code along with compiler message,then it will be better to sortout.

and please donot mention your query in the quotes also you have edited the quote of dwen hal.please keep it in mind next time.
 
Dwen Hal
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The ";" its not the problem its just a simple code that I did here, here is peace of code from my code:



 
Dave Tolls
Ranch Foreman
Posts: 3061
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's different.
That Math.pow() is not an assignment so is not allowed outside of a code block.
 
Dwen Hal
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:That's different.
That Math.pow() is not an assignment so is not allowed outside of a code block.


Aha okay thanks
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java statements, that are not declarations, must be within a constructor, initializer, or method. It can't just be placed anywhere.

[EDIT: beaten to the answer by one minute ... ]
Henry
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here you will get the error identifier expected since in order to perform Math.pow(a,3) it needs some identifier either variable or method so that you can perform the operation via accesing a variable or calling a method.think about it why have you declared Math.pow(a,b)?
 
Dave Tolls
Ranch Foreman
Posts: 3061
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
[EDIT: beaten to the answer by one minute ... ]
Henry


Go me!
Go me!

...Tolls does victory dance that many might mistake for Dad Dancing...

 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
praveen kumaar wrote:. . . it will run ofcourse . . . .
Not the first piece of code, which has a statement outside methods and constructors. That code will not compile because statements must be in side methods constructors, etc. You cannot write a statement simply in the class.
The statements shown, e.g. Math.pow(a, 3);, have no effect because their return values are not used; they simply disappear into cyber‑limbo never to be seen again. Also you sh‍ould probably not use pow for small integer arguments; the following will probably be faster:-
a * a * a
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!