Lotto with user input

Random number generated - need user input also??

Page 1 of 1

4 Replies - 1787 Views - Last Post: 19 November 2010 - 03:44 PM Rate Topic: -----

#1 stirbob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 19-November 10

Lotto with user input

Posted 19 November 2010 - 03:28 AM

Hello

Im working on an issue where i need a program to randomly generate numbers and not repeats them. I also need the progam to ask for the users input:

"How many lotto lines do you want to generate"??

So when the user put in 5 - the user gets 5 lines of lotto code

The code i have so far is :
import java.util.ArrayList;
import java.util.Random;
package javaapplication2;
/**
 *
 * @author Stirbob
 */
public class loto {

public static void main(String[] arg) {
ArrayList<Integer> al = new ArrayList<Integer>();
for(int r = 1; r <= 42; r++)
al.add(r);

Random ran = new Random();
for(int r = 0; r < 6; r++) {
int n = al.remove(ran.nextInt(al.size()));
System.out.print(" " + n);
}
System.out.println();
}  
}



Please Help??

This post has been edited by macosxnerd101: 19 November 2010 - 09:06 AM
Reason for edit:: Please use code tags.


Is This A Good Question/Topic? 0
  • +

Replies To: Lotto with user input

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Lotto with user input

Posted 19 November 2010 - 04:17 AM

You can check the Scanner class, here is the API:
http://download.orac...il/Scanner.html
declare a Scanner Object, and use the nextInt() to get user's input as int value.
Scanner input = new Scanner(System.int);
int n = input.nextInt();

Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2726
  • View blog
  • Posts: 11,465
  • Joined: 20-September 08

Re: Lotto with user input

Posted 19 November 2010 - 06:18 AM

See http://technojeeves....-random-numbers
Was This Post Helpful? 0
  • +
  • -

#4 stirbob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 19-November 10

Re: Lotto with user input

Posted 19 November 2010 - 03:12 PM

Thanks

But im still lost on the how to get it to repeat after the question is asked. I am aware of the input scanner part. I need help with the part between the question been asked and the repeating of the code... ??

Thanks
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10568
  • View blog
  • Posts: 39,131
  • Joined: 27-December 08

Re: Lotto with user input

Posted 19 November 2010 - 03:44 PM

You can use the ArrayList contains() method to check to see if an element exists in it before adding another element. Does this answer your question, or are you talking about prompting for a new set of input and repeating the program?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1