Results 1 to 2 of 2
- 02-03-2013, 08:48 PM #1
Member
- Join Date
- Feb 2013
- Posts
- 2
- Rep Power
- 0
Help passing arrays to other methods
Basically, what my program is supposed to do is generate 5 random lottery numbers, get input from the user for guesses for the numbers, then see if any of the guesses from the user are right.
I'm having errors when referencing the arrays, the values aren't being passed to the methods right.
Here's my code:
Can anyone help me out? How do I get this working?Java Code:import java.util.*; public class lottery { public static void lotteryNums(){ Random randnum=new Random(); int[] lotteryNumbers=new int[5]; for (int i=0; i<lotteryNumbers.length; i++) { //have to make the max value 51 to include 50 when using nextint lotteryNumbers[i]=(randnum.nextInt(51)+0); } //sort lotterynumbers ascending Arrays.sort(lotteryNumbers); //check lotterynumbers System.out.println("Winning numbers: " + Arrays.toString(lotteryNumbers)); } public static void lottocheck(int[] Guess) { for (int count1=0; count1<5; count1++) { for (int count2=0; count2<5; count2++) { //getting an error here for lotteryNumbers, won't recognize array if (Guess[count1]==lotteryNumbers[count2]) { int matches=matches+1; System.out.println("You have "+ matches + " matches!"); } } } } } import java.util.Arrays; import java.util.Scanner; public class lab1 { public static void main(String[] args) { getGuess(); lottery lotto=new lottery(); lotto.lotteryNums(); //Getting an error here for guesses[], won't recognize array lotto.lottocheck(guesses[]); } public static void getGuess(){ Scanner input = new Scanner(System.in); int[] guesses= new int[5]; System.out.println("Enter your 5 lottery guesses below:"); for (int i=0; i<5;i++){ guesses[i] = input.nextInt(); } //sort guesses Arrays.sort(guesses); //check guesses System.out.println("Guesses sorted: " + java.util.Arrays.toString(guesses)); } }Last edited by arfor; 02-03-2013 at 08:51 PM.
- 02-03-2013, 09:59 PM #2
Member
- Join Date
- Jan 2013
- Location
- Texas
- Posts
- 16
- Rep Power
- 0
Similar Threads
-
Passing variables to methods
By KKorn429 in forum New To JavaReplies: 3Last Post: 05-21-2012, 12:40 AM -
Passing arrays to function
By boys21 in forum New To JavaReplies: 1Last Post: 11-20-2011, 10:29 PM -
Arrays and passing values to methods help please.
By toppcon in forum New To JavaReplies: 21Last Post: 07-28-2011, 05:05 AM -
passing arguments to other methods
By popeyito18 in forum New To JavaReplies: 2Last Post: 07-02-2011, 11:01 PM -
generics and passing arrays
By TopNFalvors in forum New To JavaReplies: 0Last Post: 03-17-2011, 04:19 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks