• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why cant I user Math.random?

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1251
87
Hibernate jQuery Spring MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 491
23
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 491
23
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...

 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic