I need help, The array in Class Loto generates 6 random numbers and print them out. the Class Lotto askes the user to input 6 numbers which are stored in an array. i need code to compare the two arrays and see if they equal each other, and if they do display how many i got right. PLEASE HELP
import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class Loto { public static void main(String[] arg) { ArrayList<Integer> al = new ArrayList<Integer>(); for(int i = 1; i <= 49; i++) al.add(i); Random ran = new Random(); for(int i = 0; i < 6; i++) { int x = al.remove(ran.nextInt(al.size())); System.out.print(" " + x); } System.out.println(); } }import java.util.*; import java.util.Scanner; class lotto{ public static void main(String[]args){ int f[] = new int[6]; Scanner keyboard = new Scanner(System.in); for(int j=0; j<f.length; j++){ System.out.print("Input your " + (f[0]+1) + "st lotto number: "); f[0] = keyboard.nextInt(); } } }
well first i'd suggest changing the users int[] to an arraylist of integers also. next sort each array list check here: http://www.javamex.com/tutorials/collections/sorting_simple_cases.shtml , after you have sorted them iterate through arrays comparing each value: http://www.coderanch.com/t/407003/java/java/Compare-two-ArrayList
DavidKroukamp
Practically a Master Poster
Team Colleague
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169