Question

In java: write a lottery program that picks 4 random numbers between 1-9 and print the...

In java:

write a lottery program that picks 4 random numbers between 1-9 and print the number. If the number is a duplicate, then run it again until there are no duplicates.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Random;
public class Lottery {
  public static void main(String[] args) {
    Random random = new Random();
    int arr[] = new int[4];
    int k = 0, num, j;
    for(int i = 0;i<4;){
      num = random.nextInt(9)+1;
      for(j = 0;j<k;j++){
        if(num == arr[j]){
          break;
        }
      }
      if(j == k){
        arr[k++] = num;
        i++;
      }
    }

    for(int i = 0;i<4;i++){
      System.out.println(arr[i]);
    }
  }
}

Add a comment
Know the answer?
Add Answer to:
In java: write a lottery program that picks 4 random numbers between 1-9 and print the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT