Hey guys I'm trying to make sure when the player's number or numbers are in order with the randomly generated lottery number's they are counted correct. With my code right now, the order of the numbers does not matter and I want to change that. Thanks for reading.
public class PlayLottery {
public static void main(String[] args) {
int NUM_DIGITS = 3;
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] + " ");
System.out.println("Player numbers: " + userDigits[0] + " "
+ userDigits[1] + " " + userDigits[2] +" ");
System.out.println("Number of matching digits: " + sameNum);
if (sameNum == 3) {
System.out.println("3 numbers correct - $1000 dollars won");
}
if (sameNum == 2) {
System.out.println("2 numbers correct - $100 dollars won");
}
if (sameNum == 1) {
System.out.println("1 number correct - $10 dollars won");
}
if (sameNum == 0) {
System.out.println("0 numbers correct - $0 dollars won");
}
}
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);
return lotteryNumbers[2];
}
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();
return userDigits[2];
}
public static int compareArrays(int[] userDigits, int[] lotteryNumbers) {
int sameNum = 0;
for (int i = 0; i < 3; i++) {
for (int x = 0; x < 3; x++) {
if (lotteryNumbers[i] == userDigits[x]) {
sameNum++;
}
}
}
return sameNum;
}
}
Aucun commentaire:
Enregistrer un commentaire