2014-11-21 67 views
1

我正在爲我的班級設計一個應該模擬彩票遊戲的程序。我應該設計一種方法來生成隨機的彩票號碼,一種方法是詢問並存儲用戶的號碼選擇,一種方法是比較陣列以找出多少個號碼相同,然後我應該全部稱呼他們備份到主要方法,並創建我的輸出語句,其中包含一些if語句,用於確定針對特定匹配量頒發哪個獎項。Java彩票排列程序

這裏是我迄今

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 - $5 MILLION!!"); 
    } 

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

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

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

    if (sameNum == 1){ 
    System.out.println("WINNER - $5!!"); 
    } 
    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; 

} 

} 

我很新的陣列(和Java在那個),所以我的問題是在我的回報/調用語句。請原諒我的空間編碼風格以及我所做的任何明顯的錯誤。任何提示,建議,解決方案,或者如果您發現我有任何問題,請讓我知道。謝謝!

+0

所以,你有沒有發現任何的答案有用嗎? – 2014-11-27 02:19:24

回答

0

記住randNum.nextInt(10)會給你的彩票號碼範圍從0到9,您可以使用一個for循環隨機數分配給lotteryNumbers陣列更容易。此外,你應該確保隨機抽獎號碼不會重複。

在您的compareArrays函數中,只需將一個返回的sameNum調用放在最外面的for循環之後,否則它將不會使用正確數目的匹配數字進行更新。您需要爲compareArrays()提供正確的參數(userDigits和lotteryNumbers),並將sameNum設置爲等於此結果。

+0

謝謝,這確實有幫助。與參數有關的問題將所有東西都拋出,但現在我已經完成了所有工作。非常感謝! – Drewmeister96 2014-11-21 21:34:13

+0

很高興能幫到你!如果您對我的回答滿意,您是否將其標記爲「已接受」? – 2014-11-21 21:39:09

0

我已更新您的代碼並進行所需的更改。

  • 新增NUM_DIGITS您初始化
  • 關閉了掃描儀
  • 刪除了早期回報您的比較方法
  • 的比較方法的返回值分配給sameNum
  • 設置的返回值生成並獲得方法爲void

其他建議可能是你想要合併(suc h作爲開關/外殼)。

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 - $5 MILLION!!"); 
     } 

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

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

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

     if (sameNum == 1) { 
      System.out.println("WINNER - $5!!"); 
     } 
     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; 
    } 

} 
+0

好的,謝謝。我現在得到一個帶有彩票號碼和用戶輸入號碼的輸出,但是匹配保持爲0。我將int匹配設置爲等於我的調用語句compareArrays(sameNum),但不起作用 – Drewmeister96 2014-11-21 21:15:49

+0

您正在內循環中以及外循環中的第一個if語句之後返回,因此您將永遠無法通過數組。 – adamdc78 2014-11-21 21:19:58

+0

這很有道理,非常感謝你! – Drewmeister96 2014-11-21 21:31:00

0

幾個點,你可能會發現有用:

  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. 而是多個if的使用switch,再加上不重複整個print語句,只分配不同的部分:

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