我有2个错误使用数组编译我的java Lottery程序

时间:2015-05-06 02:56:53

标签: java 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;
}

}

当我编译时,我得到以下错误 -

   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

你们有没有人帮我解决这些编译错误?我正试图让它发挥作用。用户应该输入6个数字,程序应该随机挑选6个数字。使用这些数字,程序会将数字与回显输入进行比较。

2 个答案:

答案 0 :(得分:0)

generateNumbersgetUserDatavoid个函数,这意味着它们不会返回任何内容,因此您无法从它们返回任何内容。

您可能希望将它们声明为返回int的函数:

public static int generateNumbers(int[] lotteryNumbers)

答案 1 :(得分:0)

Univerio回答原始问题是正确的。

查看您的测试代码,您可能会考虑删除这两个函数的return语句,因为您只是填充两个数组。