我正在爲我的班級設計一個應該模擬彩票遊戲的程序。我應該設計一種方法來生成隨機的彩票號碼,一種方法是詢問並存儲用戶的號碼選擇,一種方法是比較陣列以找出多少個號碼相同,然後我應該全部稱呼他們備份到主要方法,並創建我的輸出語句,其中包含一些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在那個),所以我的問題是在我的回報/調用語句。請原諒我的空間編碼風格以及我所做的任何明顯的錯誤。任何提示,建議,解決方案,或者如果您發現我有任何問題,請讓我知道。謝謝!
所以,你有沒有發現任何的答案有用嗎? – 2014-11-27 02:19:24