制作彩票应用程序:使用随机数填充数组

时间:2013-04-26 23:37:52

标签: java class random static

我正在用Java制作彩票应用程序。我的问题是,我认为一切都已到位,它(IDE​​)告诉我“int lotteryNumbersCount = Eck_LotteryClass.getLotteryNumbers().length;”需要是静态的。所以我将它改为静态int,然后我必须在我的班级再次更改它。问题是,当我最终运行它时,我得到所有0的随机抽奖数据。请帮助我找到我的方式错误。在这里总和newb,我一直在网上寻找,但我想尝试解决它,而不只是在某处复制代码。

Eck_LotteryClass

import java.util.Random;



public class Eck_LotteryClass {
//instance field
private  int lotteryNumbers [];


//Create random lottery numbers method array
public  int [] getRandomNumbers(){
    lotteryNumbers = new int [5];
    Random r = new Random();
    for(int i = 0; i < 5; i++)
        lotteryNumbers[i] = r.nextInt(10);

    return lotteryNumbers;
}



public int compareNumbers(int[] usersNumbers) {

            int matchedNums = 0;
            if (usersNumbers.length == lotteryNumbers.length) {
                for (int i = 0; i < lotteryNumbers.length; i++) {
            if (usersNumbers[i] == lotteryNumbers[i]) {
                matchedNums ++;
                    }
                }
            }

            return matchedNums;}

// Display the random lottery numbers for the user
public int [] getLotteryNumbers() {
    return lotteryNumbers;
}




}

Eck_LotteryTester

import java.util.Scanner;
import java.util.Arrays;


public class Eck_LotteryTester{

public static void main(String[] args) {

    Eck_LotteryClass lottery = new Eck_LotteryClass();

    int lotteryNumbersCount = Eck_LotteryClass.getLotteryNumbers().length;

    System.out.println("The Pennsylvania Lottery\n");
    System.out.println("There are " + lotteryNumbersCount
            + " numbers in my lottery, they are 0 through 9. "
            + "See if you can win big CASH prizes!!!\n");

    // Asks the user to enter five numbers.
    Scanner keyboard = new Scanner(System.in);
    int numbers[] = new int[lotteryNumbersCount];

    for (int index = 0; index < numbers.length; index++) {
        System.out.print(String.format("Enter Number %d: ", index + 1));
        numbers[index] = keyboard.nextInt();
    }

    // Display the number of digits that match the randomly generated
    // lottery numbers.

    int match = lottery.compareNumbers(numbers);

    if (match == lotteryNumbersCount) {

        // If all of the digits match, display a message proclaiming the
        // user a grand prize winner.
        System.out.println("\nYOU WIN, GO SEE D. LEETE FOR YOUR GRAND PRIZE!!!");

    } else {

        System.out.println("\nThe winning numbers are " +  Arrays.toString(Eck_LotteryClass.getLotteryNumbers()) + 
                "\nYou matched " + match + " number(s).");

    }

  }
}

2 个答案:

答案 0 :(得分:2)

更改

  int lotteryNumbersCount = Eck_LotteryClass.getLotteryNumbers().length;

  int lotteryNumbersCount = lottery .getLotteryNumbers().length;

并且您不必将方法签名更改为静态。你也将谈论同一个变量。

同时更改

// Display the random lottery numbers for the user
public int [] getLotteryNumbers() {
    return lotteryNumbers;
}

    // Display the random lottery numbers for the user
public int [] getLotteryNumbers() {
    return getRandomNumbers();
}

因此数组被初始化。并改变

的签名
public  int [] getRandomNumbers

private  int [] getRandomNumbers

不会受伤

答案 1 :(得分:0)

package New_list;
import java.util.Scanner;
import java.util.Random;

public class Lottery {
    private static Scanner scan;

    public static void main(String[] args) {
        System.out.println("\t\t\tWelcome to Harsh Lottery System.\n");
        Random random = new Random();
        int lottery_win_1 = random.nextInt(10);
        // Print Lottery winning number...1 :P
        // System.out.println(lottery_win_1 + "\n");
        int lottery_win_2 = random.nextInt(10);
        // Print Lottery winning number...2 :P
        // System.out.println(lottery_win_2 + "\n");
        boolean loop = true;
        while(loop){
            System.out.println("\t\t\tEnter your 2 Digit Lottery number.\n");
            scan = new Scanner(System.in);
            int lottery_no = scan.nextInt();
            if ((lottery_no >= 0) && (lottery_no <= 99)) {
                int lottery_no_1, lottery_no_2;
                if (lottery_no > 9) {
                    lottery_no_1 = lottery_no / 10;
                    lottery_no_2 = lottery_no % 10;
                } else {
                    lottery_no_1 = 0;
                    lottery_no_2 = lottery_no;
                }
                if ((lottery_win_1 == lottery_no_1)
                    && (lottery_win_2 == lottery_no_2)) {
                    System.out
                        .println("\t\t\tCongratulation you win lottery,and you win $10000.\n");
                } else if ((lottery_win_1 == lottery_no_2)
                           && (lottery_win_2 == lottery_no_1)) {
                    System.out
                        .println("\t\t\tCongratulation your inverse no is lottery winer number so that you win $4000.\n");
                } else if ((lottery_win_1 == lottery_no_1)
                           || (lottery_win_1 == lottery_no_2)
                           || (lottery_win_2 == lottery_no_1)
                           || (lottery_win_2 == lottery_no_2)) {
                    System.out
                        .println("\t\t\tCongratulation your one digit from your lotter number match to the lottery winner.so you win $1000.\n");
                } else {
                    System.out.println("\t\t\tSorry,Please try again\n");
                    System.out.println("\t\t\tDo you want to try again\n\t\t\tPress 1 for Continue\n\t\t\tPress 2 for exit\n");
                    int ch = scan.nextInt();
                    switch(ch){
                    case 1: System.out.println("\t\t\tOk...Try again\n");
                        break;
                    case 2: System.out.println("\t\t\tBbye... See you later\n");
                        loop = false;
                        break;
                    }
                }
            } else {
                System.out.println("\t\t\tSorry,Please choose 2 digit number\n");
            }
        }
    }
}