java - How to compare existing numbers from a file with with a random number? -
I am doing a practice where I have to do a lotto game.
We have 6 random numbers ( rand1
, rand2
, rand3
, rand4
, Rand5
generated, rand6
), and now I want to compare those my current numbers, which are saved in the file f
.
This is what I have so far:
public square lottoEx {public static zero main (string [] args) exceptions {userNumbers (4, 5, 7, 9, 11, 19) throws; DrawNumbers (); } Public static zero user number (int no 1, int number 2, int number 3, int number 4, int number 5, int number 6) exception {// USER NUMBERS file F = new file ("lotto") throws ; Printstream output = new printstream (f); Output.println (num1 + "+ + num2 +" "+ num3 +" "+ num4 +" "+ num5 +" "+ num6); read scanner = new scanner (f); System.out.print (" Your number : "); While (read.hasNext ()) {System.out.print (read.nextInt () +" ");}} Public static zero draw () throws an exception {// Random Number Random Rand = New Random (); Int rand1 = rand.nextInt (19) +1; int rand2 = rand.nextInt (19) +1; int rand3 = rand.nextInt (19) +1; int rand4 = rand.nextInt (19) +1 ; Int rand5 = rand.nextInt (19) +1; int rand6 = rand.nextInt (19) +1; Thread.Sleep (2000); Println (); Println (); System.out.print ("There are lotto numbers : "); Thread.Sleep (2000); System.out.print (rand1 +" + + rand2 + "" + rand3 + "" + rand4 + "" + rand5 + "" + rand6); DoCompare (rand1, rand2, rand3, rand4, rand5, rand6); WinCheck (); } Public static zero doCompare (int rand1, int rand2, int rand3, int rand4, int rand5, int rand6) throws an exception {scanner scan = new scanner ("lotto.dat"); Set up & lt; Integer & gt; Set = new hashet & lt; & Gt; (); While (scan.hasNextInt ()) {set.add (scan.nextInt ()); } System.out.println (set); If (set contains (rand1)) {System.out.println ("match on rand1"); }} Public static zero winCheck () throws exceptions {// code goes here}}
It seems that some things are wrong because it only random numbers with the first number in my file Does it compare? I'm stuck now Hope someone can help me! : -)
An easy and an effective way to set a to all numbers in the file
, and then see the set for each random number. In this way you only have to read the file once.
set & lt; Integer & gt; Set = new hashet & lt; & Gt; (); While (scan.hasNextInt ()) {set.add (scan.nextInt ()); } If (set. (Rand1)) {System.out.println ("match on Rand 1:" + Rand 1); } // Repeat for Rand 2, rand3..etc
Comments
Post a Comment