mardi 5 mai 2015

I have 2 errors compiling my java Lottery program using arrays

import java.util.Random;
import java.util.Scanner;

public class LotteryGame{

public static void main(String[] args) {

    int NUM_DIGITS = 6;

    int[] userDigits = new int[NUM_DIGITS];
    int[] lotteryNumbers = new int[NUM_DIGITS];
    int sameNum;

    generateNumbers(lotteryNumbers);
    getUserData(userDigits);
    sameNum = compareArrays(lotteryNumbers, userDigits);

    System.out.println("Winning numbers: " + lotteryNumbers[0] + " "
            + lotteryNumbers[1] + " " + lotteryNumbers[2] + " "
            + lotteryNumbers[3] + " " + lotteryNumbers[4] + " " + lotteryNumbers[5] + " ");

    System.out.println("Your numbers:  " + userDigits[0] + " "
            + userDigits[1] + " " + userDigits[2] + " " + userDigits[3]
            + " " + userDigits[4] + " " + userDigits[5] +" ");
    System.out.println("Number of matching digits: " + sameNum);

    if (sameNum == 6) {
        System.out.println("First prize!!!");
    }

    if (sameNum == 5) {
        System.out.println("Second prize!!!");
    }
    if (sameNum == 0) {
        System.out.println("No matching numbers, you lost.");
    }

}

public static void generateNumbers(int[] lotteryNumbers) {

    Random randNum = new Random();

    lotteryNumbers[0] = randNum.nextInt(59);
    lotteryNumbers[1] = randNum.nextInt(59);
    lotteryNumbers[2] = randNum.nextInt(59);
    lotteryNumbers[3] = randNum.nextInt(59);
    lotteryNumbers[4] = randNum.nextInt(59);
    lotteryNumbers[5] = randNum.nextInt(59);

    return lotteryNumbers[5];
}

public static void getUserData(int[] userDigits) {
    Scanner keyboard = new Scanner(System.in);

    System.out.print("Enter first digit: ");
    userDigits[0] = keyboard.nextInt();
    System.out.print("Enter second digit: ");
    userDigits[1] = keyboard.nextInt();
    System.out.print("Enter third digit: ");
    userDigits[2] = keyboard.nextInt();
    System.out.print("Enter fourth digit: ");
    userDigits[3] = keyboard.nextInt();
    System.out.print("Enter fifth digit: ");
    userDigits[4] = keyboard.nextInt();
    System.out.print("Enter sixth digit: ");
    userDigits[5] = keyboard.nextInt();



    return userDigits[5];
}

public static int compareArrays(int[] userDigits, int[] lotteryNumbers) {
    int sameNum = 0;

    for (int i = 0; i < 6; i++) {
        for (int x = 0; x < 5; x++) {
            if (lotteryNumbers[i] == userDigits[x]) {
                sameNum++;
            }
        }
    }
    return sameNum;
}

}

When I compile I get the following errors-

   LotteryGame.java:51: error: incompatible types: unexpected return value
       return lotteryNumbers[5];
                         ^
   LotteryGame.java:72: error: incompatible types: unexpected return value
    return userDigits[5];
                     ^
      2 errors

Can any of you help me with these compilation errors? I'm trying to get this to work. The user is supposed to input 6 numbers, and the program is supposed to pick randomly 6 numbers. Using these numbers, the program will compare the numbers with echoed input.

Aucun commentaire:

Enregistrer un commentaire