Welcome to Dream.In.Code
Become a Java Expert!

Join 149,413 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,220 people online right now. Registration is fast and FREE... Join Now!




Methods for Pick a Number

 
Reply to this topicStart new topic

Methods for Pick a Number, Pick a Number between 1-30 with 3 methods in code

g79reg
15 Mar, 2007 - 11:28 AM
Post #1

New D.I.C Head
*

Joined: 15 Mar, 2007
Posts: 1


My Contributions
I have to type a program were the computer will pick a random number between 1-30. We are to use three methods rndPick() getNumApart(???) and getLuck(???).
The computer will pick a # the use guess what computer picked. User then guesses how close they think they were to the computer. How do I call these methods? PLEASE HELP
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Methods For Pick A Number
15 Mar, 2007 - 11:40 AM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,347



Thanked: 51 times
Dream Kudos: 25
My Contributions
Were you provided with any additional information for your assignment? Also , the site prefers to see a good faith effort on the part of the user. Can you post what you've written so far?
User is online!Profile CardPM
+Quote Post

k0rrup7
RE: Methods For Pick A Number
17 Mar, 2007 - 10:41 AM
Post #3

New D.I.C Head
*

Joined: 16 Feb, 2007
Posts: 16


My Contributions
Hey, I'll help you a little bit, but as Amadeus said, i prefer to see a good faith effort on your part.
Anyways, I'll help you a little bit.

What you can do to let the computer pick a random number is to use the java.util.Random class.

You import it like this, don't forget to make an object!

CODE

import java.util.Random;

Random Object = new Random();


User is offlineProfile CardPM
+Quote Post

aRTx
RE: Methods For Pick A Number
18 Mar, 2007 - 12:43 PM
Post #4

New D.I.C Head
*

Joined: 2 Dec, 2006
Posts: 2


My Contributions
// by aRTx
public class TryIt
{
public static void main(String[] args)
{
int nri = -5;
while(!(nri > 0 && nri <= 30))
{
double nrd = Math.random();
nrd = nrd * 100;
nri = (int)nrd;
System.out.println ("not " + nri);

}
System.out.println (nri);
}
}
User is offlineProfile CardPM
+Quote Post

capty99
RE: Methods For Pick A Number
18 Mar, 2007 - 07:25 PM
Post #5

the real kya
Group Icon

Joined: 26 Apr, 2001
Posts: 9,248



Thanked: 16 times
Dream Kudos: 550
My Contributions
real quick off the bat,
im in a java class right now,
and alot of professors will take off for not have descriptive variable names,
just so you know
nri and nrd as real words would make the whole program more readable.

CODE

//Define Random generator and picks a random number
    Random generator = new Random();
    int realNum = generator.nextInt(100)+1;


thats an example of a random number picker,
(note that the nextInt(100) will return a random number from 0-99, so the plus one shifts the field to 1 through 100.


from that you want to ask the user for a guess,
with the scanner class most likely,

then do a ladder if else
if the realNum is greater do one thing
if its less do another,
and if its equal display the user a reward.


User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 10:53AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month