
July 15th, 2007, 02:27 PM
|
|
Registered User
|
|
Join Date: Jul 2007
Posts: 1
Time spent in forums: 9 m 51 sec
Reputation Power: 0
|
|
|
Lottery problem...
I'm suppose to create a program where the user inputs 6 numbers from 1-49,
the program then gets 6 random numbers
matching the random numbers and the user input numbers to find how many number matches...
but I can't get it to work...
Code:
import java.util.Scanner;
public class PlayLottery {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] userNum = getUserNumbers();
int[] randomNum = getRandomNumbers();
int matchedNum = getTotalMatchedNumbers(userNum, randomNum);
for (int i = 0; i < 6; i++) {
System.out.print("There are " + matchedNum + " matched number(s)");
}
}
public static int[] getUserNumbers() {
Scanner scanner = new Scanner(System.in);
int[] userNum = new int[6];
for (int i = 0; i < userNum.length; i++){
System.out.print("Enter 6 different numbers: ");
userNum[i] = scanner.nextInt();
}
return userNum;
}
public static int[] getRandomNumbers() {
int[] randomNum = new int[6];
for (int i = 0; i < randomNum.length; i++){
randomNum[i] = (int)(Math.random()*2);
for (int j = 0; j < i; ++j){
if (randomNum[i] == randomNum[j]){
--i;
}
}
}
return randomNum;
}
public static int getTotalMatchedNumbers(int[] userNum, int[] randomNum) {
int matchedNum = 0;
for (int i = 0; i < 6; i++){
if (userNum[i] == randomNum[i]){
matchedNum++;
} else {
}
}
return matchedNum;
}
}
|