Results 1 to 15 of 15
- 07-08-2011, 08:38 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 4
- Rep Power
- 0
Random Number Generator with User Input
Hi,
I'm new to Java and I'm making a really simple random number generator, but I also want for the user to be able to choose the maximum number e.g. Random Number between 1-100, but the problem is I'm not sure of how to say it's an integer field, and I know that java is probably expecting that it'd be like 3 * hello which wouldn't work. Currently I've got the code;
I'm guessing that this'll be a really easy fix but I'm not exactly sure where to go. Thanks for your help.Java Code:import java.util.Scanner; public class ChoiceofRandomNumber { public static void main(String[] args) { Scanner r = new Scanner(System.in); System.out.println("What's the Maximum Number you Want to be Possible?"); String name = r.nextLine(); System.out.println((int) (Math.random() * r) +1); } }
- 07-08-2011, 08:49 PM #2
Is that what you're trying to do? Make a scanner input that only accepts integers?Java Code:int myInt = r.nextInt();
- Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
- +Rep people for helpful posts.
- 07-08-2011, 08:53 PM #3
Member
- Join Date
- Jul 2011
- Posts
- 4
- Rep Power
- 0
Yeah that's exactly it! Thanks for the quick response.
- 07-08-2011, 08:56 PM #4
Ok well 11 minutes isn't really slow, you could have found this out even faster by using Google. What I like to do, and actually how I found what you were looking for was by using Google.
I typed in "Scanner class java 6 api" and this is the first link that showed up:
Scanner (Java Platform SE 6)- Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
- +Rep people for helpful posts.
- 07-08-2011, 09:06 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
Even better: download the entire API documentation from this link. It isn't much (mega-byte size speaking) but it's valuable stuff and having it all available (even off-line) takes away all those lame excuses where people simply guess, try and hope for the best. b.t.w. the download link can be found in the left margin, down the page.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 07-08-2011, 09:14 PM #6
@JosAH, good idea. I can't do that right now, but is there a search feature in the offline version?
- Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
- +Rep people for helpful posts.
- 07-08-2011, 09:40 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
- 07-08-2011, 09:41 PM #8
Member
- Join Date
- Jul 2011
- Posts
- 4
- Rep Power
- 0
Downloading now (56.4MB) but the server seems to be quite slow. Thanks again.
- 07-08-2011, 09:47 PM #9
Ok, thanks JosAH. I will keep that in mind for future reference.
- Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
- +Rep people for helpful posts.
- 07-09-2011, 07:27 AM #10
Why dont you look at the above code.I am also new to JAVA and like to wite programs.Though i know it's bit complex code which i have written but i have just given a try.Pls do comment about it.Java Code:import java.util.*; class Random1 { public static void main(String args[]) { Random ar=new Random(); Scanner read=new Scanner(System.in); int n; System.out.println("Enter min value"); int min=read.nextInt(); System.out.println("Enter max value"); int max=read.nextInt(); if(max==min) System.out.println("Random number between "+min+ "-" +max+" is "+max); do { n=ar.nextInt(max); if(n>=min) System.out.println("Random number between "+min+ "-" +max+" is "+n); } while(n<min); } }
Last edited by DarrylBurke; 07-09-2011 at 08:31 AM. Reason: Removed annoying all-bold formatting
- 07-09-2011, 07:53 AM #11
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
I don't know if you are looking for help or spoon-feeding, if the former, please make your own thread, otherwise, don't spoonfeed! Also, if you do post code, use code tags, not quote tags
[code]
YOUR CODE HERE
[/code]
- 07-09-2011, 08:18 AM #12
Hey , i am new to this forum ,I just joined today so was not knowing it and yes i am not seekin for any help ,i was just helping the thread owner to have look at the code whether it was this ,he was looking for and yes next time will use that code tag
Last edited by DarrylBurke; 07-09-2011 at 08:31 AM. Reason: Removed annoying all-bold formatting
- 07-09-2011, 09:02 AM #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
- 07-09-2011, 09:03 AM #14
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
That's fine to be new, but it will help the user if you just tell him what to use and let him do the work. For instance, on this Problem he will probably want to use Scanner, Random, and a loop, which can all be easily found in the API.
Other than that, have fun here and don't get offended if people correct you.
If you want to find a range you will want to find a number from ((max-min)+1)+min.
If min is 20, max is 40, you would find numbers in the range 0-20(40-20 + 1) and add 20 to it.
Java Code:nextInt(21); //generates numbers from 0(inclusive) to 21(exclusive)
Last edited by sunde887; 07-09-2011 at 09:09 AM.
- 07-10-2011, 10:49 AM #15
Similar Threads
-
Random Phone number generator
By elecleoalune in forum New To JavaReplies: 13Last Post: 04-20-2011, 09:47 AM -
Help with Random Number Generator
By celtics in forum New To JavaReplies: 0Last Post: 03-07-2011, 08:18 PM -
Random number generator
By zerwik in forum New To JavaReplies: 3Last Post: 12-26-2010, 12:10 PM -
Random number generator
By Michailangelo in forum Advanced JavaReplies: 4Last Post: 04-02-2010, 06:47 PM -
Help with class project, random number generator.
By Christopher The Great in forum New To JavaReplies: 4Last Post: 03-14-2009, 02:12 AM


LinkBack URL
About LinkBacks
Reply With Quote
.gif)

Bookmarks