Methods for Pick a Number

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

Page 1 of 1

4 Replies - 2208 Views - Last Post: 18 March 2007 - 08:25 PM Rate Topic: -----

#1 g79reg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 15-March 07

Methods for Pick a Number

Posted 15 March 2007 - 12:28 PM

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
Is This A Good Question/Topic? 0
  • +

Replies To: Methods for Pick a Number

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Methods for Pick a Number

Posted 15 March 2007 - 12:40 PM

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?
Was This Post Helpful? 0
  • +
  • -

#3 k0rrup7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 16-February 07

Re: Methods for Pick a Number

Posted 17 March 2007 - 11:41 AM

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!

import java.util.Random;

Random Object = new Random();



Was This Post Helpful? 0
  • +
  • -

#4 aRTx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 02-December 06

Re: Methods for Pick a Number

Posted 18 March 2007 - 01:43 PM

// 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);
}
}
Was This Post Helpful? 0
  • +
  • -

#5 capty99  Icon User is offline

  • i am colt mccoy
  • member icon

Reputation: 98
  • View blog
  • Posts: 10,081
  • Joined: 26-April 01

Re: Methods for Pick a Number

Posted 18 March 2007 - 08:25 PM

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.

//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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1