Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Generating a random number  RSS feed

 
Cory Marcus
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm trying to generate a random number between 1 and 10.

This is my code so far.



I don't know why this isn't printing to the console.
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Main method must be static.
 
Cory Marcus
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Pawel,

Although I'm now receiving the error:

"Non-Static method randomInteger(int,int) cannot be referenced from a static context"
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is because you can not call non-static method from a static one.
You have two options:
1. Make randomInteger() static.
2. Create an instance of GamePlay and call randomInteger() on the instance.
 
Cory Marcus
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Pawel,

I've made these changes, but I still receive no printout to the console.

This is the updated code:

 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It works for me.
How do you compile/run this? Copy&paste the console output.
 
Cory Marcus
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
run:
BUILD SUCCESSFUL (total time: 0 seconds)
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you using an IDE? Which one? Make sure you are running correct class.
And I think a good text editor and a console (javac, java commands) are better for learning.
 
Cory Marcus
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm also using Netbeans
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!