我正在为我的课程设计一个应该模拟彩票游戏的程序。我应该设计一种生成随机抽奖号码的方法,一种询问和存储用户数量选择的方法,一种比较数组以查找数量相同的方法,然后我应该将它们全部称为备份到main方法,并创建我的输出语句,其中包含一些if语句,用于确定特定匹配数量的奖项。
这是我到目前为止所拥有的
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)非常陌生,所以我的问题出在我的return / call语句中。请原谅我的空间编码风格和我所犯的任何明显错误。任何提示,建议,解决方案,或者如果您发现我有什么问题请告诉我。谢谢!
答案 0 :(得分:0)
我已根据您需要的更改更新了您的代码。
其他建议可能是您想要合并的内容(例如开关/案例)。
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;
}
}
答案 1 :(得分:0)
请记住,randNum.nextInt(10)会为您提供从0到9的彩票号码。您可以使用for循环将随机数分配给lotteryNumbers数组更容易。此外,您应该确保随机抽奖号码不会重复。
在你的compareArrays函数中,只需在最外面的for循环之后返回一个sameNum调用,否则它将不会使用正确的匹配数字进行更新。您需要为compareArrays()提供正确的参数(userDigits和lotteryNumbers),并将sameNum设置为等于此结果。
答案 2 :(得分:0)
您可能会觉得有用的几点:
您可能希望使用NUM_DIGITS
来启动数组,因为这是命名常量的全部要点:
int[] userDigits = new int[NUM_DIGITS];
int[] lotteryNumbers = new int[NUM_DIGITS];
您可以使用Arrays.toString()
输出数组,例如:
System.out.println(Arrays.toString(lotteryNumbers));
使用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);