java Java彩票阵列程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27070169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:09:28  来源:igfitidea点击:

Java Lottery Array Program

javaarrays

提问by Drewmeister96

I am designing a program for my class that is supposed to simulate a lottery game. I am supposed to design a method that generates random lottery numbers, a method that asks and stores the user for their number choices, a method that compares the arrays to find how many numbers are the same, and then I am supposed to call them all back up to the main method, and create my output statement that contains some if statements that determine which prize is awarded for the particular amount of matches.

我正在为我的班级设计一个程序,该程序应该模拟彩票游戏。我应该设计一种生成随机彩票号码的方法,一种询问并存储用户选择号码的方法,一种比较数组以找出有多少相同号码的方法,然后我应该将它们全部调用备份到 main 方法,并创建我的输出语句,其中包含一些 if 语句,用于确定为特定数量的匹配授予哪个奖品。

Here is what I have thus far

这是我迄今为止所拥有的

import java.util.*;


public class LotteryGame {
/**
  The main method is the program's starting point 
*/
public static void main(String[] args){

  int NUM_DIGITS = 5;

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

  generateNumbers(lotteryNumbers);
  getUserData(userDigits);
  compareArrays();


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

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

  if (sameNum == 5){
     System.out.println("GRAND PRIZE WINNER -  MILLION!!");
     }

  if (sameNum == 4){
     System.out.println("SUPER PRIZE WINNER - 0,000!!");
     }

  if (sameNum == 3){
     System.out.println("GOOD PRIZE WINNER - ,000!!");
     }

  if (sameNum == 2){
     System.out.println("NICE PRIZE WINNER - 0!!");
     } 

  if (sameNum == 1){
     System.out.println("WINNER - !!");
  }
  if (sameNum ==0){
     System.out.println("No matching numbers - better luck next time");
     }


} 
public static int generateNumbers(int [] lotteryNumbers){


  Random randNum = new Random();

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

  return lotteryNumbers[4];
}

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

  System.out.print("Enter digit 1: ");
  userDigits[0] = keyboard.nextInt();
  System.out.print("Enter digit 2: ");
  userDigits[1] = keyboard.nextInt();
  System.out.print("Enter digit 3: ");
  userDigits[2] = keyboard.nextInt();
  System.out.print("Enter digit 4: ");
  userDigits[3] = keyboard.nextInt();
  System.out.print("Enter digit 5: ");
  userDigits[4] = keyboard.nextInt();

  return userDigits[4];

}

public static int compareArrays (int [] userDigits, 
                                    int [] lotteryNumbers){

 int sameNum = 0;

 for (int i = 0; i < 5; i++){

     for (int x = 0; x < 5; x++){

         if (lotteryNumbers[i] == userDigits[x]){
           sameNum++;
           }
              return sameNum;
              }                           

              return sameNum;                         
  }
              return sameNum;

 }

}

I am very new to arrays (and Java at that) so my problems are in my return/call statements. Please excuse my spacey coding style and any blatant mistakes that I have made. Any tips,advice, solutions, or if you notice anything wrong with what I have please let me know. Thanks!

我对数组(和 Java)非常陌生,所以我的问题出在我的返回/调用语句中。请原谅我空洞的编码风格和我犯的任何明显错误。任何提示、建议、解决方案,或者如果您发现我所拥有的有任何问题,请告诉我。谢谢!

采纳答案by not_a_bot

Keep in mind that randNum.nextInt(10) will give you lottery numbers that range from 0 to 9. You can use a for loop to assign the random numbers to the lotteryNumbers array easier. Also, you should make sure that the random lottery numbers don't repeat.

请记住, randNum.nextInt(10) 将为您提供范围从 0 到 9 的彩票号码。您可以使用 for 循环更轻松地将随机数分配给 lotteryNumbers 数组。此外,您应该确保随机彩票号码不会重复。

In your compareArrays function, just put one return sameNum call after the outermost for loop, otherwise it won't update with the correct number of matching numbers. You need to give compareArrays() the correct parameters (userDigits and lotteryNumbers) and set sameNum equal to this result.

在您的 compareArrays 函数中,只需在最外层的 for 循环之后放置一个 return sameNum 调用,否则它将不会更新为正确数量的匹配数字。您需要为 compareArrays() 提供正确的参数(userDigits 和 lotteryNumbers)并将 sameNum 设置为等于该结果。

回答by adamdc78

I've updated your code with the changes you'll need.

我已经用您需要的更改更新了您的代码。

  • Added NUM_DIGITS to your initializer
  • Closed your Scanner
  • Removed the early returns in your comparison method
  • Assigned the return value of the comparison method to sameNum
  • Set the return value of the generation and get methods to void
  • 将 NUM_DIGITS 添加到您的初始值设定项
  • 关闭您的扫描仪
  • 删除了比较方法中的早期回报
  • 将比较方法的返回值赋给 sameNum
  • 将生成和get方法的返回值设置为void

The other suggestions might be something you want to incorporate (such as the switch/case).

其他建议可能是您想要合并的内容(例如 switch/case)。

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

public class App {

    public static void main(String[] args) {

        int NUM_DIGITS = 5;

        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("Lottery numbers: " + lotteryNumbers[0] + " "
                + lotteryNumbers[1] + " " + lotteryNumbers[2] + " "
                + lotteryNumbers[3] + " " + lotteryNumbers[4] + " ");

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

        if (sameNum == 5) {
            System.out.println("GRAND PRIZE WINNER -  MILLION!!");
        }

        if (sameNum == 4) {
            System.out.println("SUPER PRIZE WINNER - 0,000!!");
        }

        if (sameNum == 3) {
            System.out.println("GOOD PRIZE WINNER - ,000!!");
        }

        if (sameNum == 2) {
            System.out.println("NICE PRIZE WINNER - 0!!");
        }

        if (sameNum == 1) {
            System.out.println("WINNER - !!");
        }
        if (sameNum == 0) {
            System.out.println("No matching numbers - better luck next time");
        }

    }

    public static void generateNumbers(int[] lotteryNumbers) {

        Random randNum = new Random();

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

        return lotteryNumbers[4];
    }

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

        System.out.print("Enter digit 1: ");
        userDigits[0] = keyboard.nextInt();
        System.out.print("Enter digit 2: ");
        userDigits[1] = keyboard.nextInt();
        System.out.print("Enter digit 3: ");
        userDigits[2] = keyboard.nextInt();
        System.out.print("Enter digit 4: ");
        userDigits[3] = keyboard.nextInt();
        System.out.print("Enter digit 5: ");
        userDigits[4] = keyboard.nextInt();

        keyboard.close();

        return userDigits[4];
    }

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

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

}

回答by striving_coder

Several points you might find useful:

您可能会觉得有用的几点:

  1. You might want to use NUM_DIGITSfor the initiation of the arrays since that's the whole point of having named constant:

    int[] userDigits = new int[NUM_DIGITS];
    int[] lotteryNumbers = new int[NUM_DIGITS];
    
  2. You can use Arrays.toString()to output arrays, e.g.:

    System.out.println(Arrays.toString(lotteryNumbers));
    
  3. Instead of multiple ifs use switch, plus do not repeat the whole print statement, only assign the part that differs:

    String prize = "";
    switch (sameNum) {
        case 5:  prize = "GRAND PRIZE WINNER -  MILLION!!";
                 break;
        case 4:  prize = "SUPER PRIZE WINNER - 0,000!!";
                 break;
        case 3:  prize = "GOOD PRIZE WINNER - ,000!!";
                 break;
        case 2:  prize = "NICE PRIZE WINNER - 0!!";
                 break;
        case 1:  prize = "WINNER - !!";
                 break;
        case 0:  prize = "No matching numbers - better luck next time";
                 break;
        default: prize = "Something weird happened";
    }
    System.out.println(prize);
    
  1. 您可能希望NUM_DIGITS用于数组的初始化,因为这是命名常量的全部意义:

    int[] userDigits = new int[NUM_DIGITS];
    int[] lotteryNumbers = new int[NUM_DIGITS];
    
  2. 您可以使用Arrays.toString()输出数组,例如:

    System.out.println(Arrays.toString(lotteryNumbers));
    
  3. 而不是多个ifs use switch, plus 不要重复整个打印语句,只分配不同的部分:

    String prize = "";
    switch (sameNum) {
        case 5:  prize = "GRAND PRIZE WINNER -  MILLION!!";
                 break;
        case 4:  prize = "SUPER PRIZE WINNER - 0,000!!";
                 break;
        case 3:  prize = "GOOD PRIZE WINNER - ,000!!";
                 break;
        case 2:  prize = "NICE PRIZE WINNER - 0!!";
                 break;
        case 1:  prize = "WINNER - !!";
                 break;
        case 0:  prize = "No matching numbers - better luck next time";
                 break;
        default: prize = "Something weird happened";
    }
    System.out.println(prize);