Creating a java program involving four prize sums

Joined
Nov 21, 2021
Messages
1
Reaction score
1
Hey. So I'm currently studying loops and arrays in Java in my college class. I have an assigment and I've completed most of the questions but I'm stumped on one question in particular. It involves creating four prize sums and if the sum appears more than once, the user wins. Here is the question (question 3) in greater detail:

iBcij5P (1).png


This was my attempt at it but it doesn't seem to work. Could someone point out where I went wrong?


public static int GeneratePrize() {

int prizeNumber = (int)(Math.random() * 99 + 1);

if (prizeNumber >= 0 || prizeNumber <= 50) {

System.out.println("You won €10");
}

if (prizeNumber > 50 || prizeNumber <= 77) {

System.out.println("You won €20");
}

if (prizeNumber > 77 || prizeNumber <= 92) {

System.out.println("You won €50");

}
if (prizeNumber > 92 || prizeNumber <= 98 ) {

System.out.println("You won €100");

}
if (prizeNumber > 98 || prizeNumber <=100) {

System.out.println("You won €");
}
return prizeNumber;
 
Joined
Mar 3, 2021
Messages
240
Reaction score
30
Hello and welcome! Sorry I'm a bit late, are you still working on this? The main problem is that you're using ORs (||) instead of ANDs (&&), so you'll only ever generate 10 Euros. To actually use that value, I'd recommend the following. Instead of returning the random number, return an index for an array so you can count up each one. Then, look through the array and check if any were returned more than once. If they were, look up what prize that index is worth. As an aside, you can actually remove all of the greater-than or greater-than-or-equal to checks, since each block makes that check moot.

Java:
class Main {
        public static int GeneratePrizeSum() {
                int prizeNumber = (int)(Math.random() * 99 + 1);
                if (prizeNumber >= 0 && prizeNumber <= 50) {
                        System.out.println("You won €10");
                        return 0;
                }
                if (prizeNumber > 50 && prizeNumber <= 77) {
                        System.out.println("You won €20");
                        return 1;
                }
                if (prizeNumber > 77 && prizeNumber <= 92) {
                        System.out.println("You won €50");
                        return 2;
                }
                if (prizeNumber > 92 && prizeNumber <= 98 ) {
                        System.out.println("You won €100");
                        return 3;
                }
                if (prizeNumber > 98 && prizeNumber <=100) {
                        System.out.println("You won €1000");
                        return 4;
                }
                return -1;
        }

        public static int GeneratePrize(){
                final int sumValues[] = new int[]{10, 20, 50, 200, 1000};
                int sums[] = new int[5];
                for (int i=0;i<5;i++){
                        sums[GeneratePrizeSum()]++;
                }
                int total = 0;
                for (int i=0;i<5;i++){
                        if (sums[i] >= 2){
                                total += sumValues[i];
                        }
                }
                return total;
        }
        public static void main(String[] args){
                System.out.println("Total bonus: €" + GeneratePrize());
        }
}
 
Joined
Mar 17, 2022
Messages
2
Reaction score
1
lottery.png

Question #1. Generate 10 random winning numbers from 1 to 99. Solved using a generateArray function & a Sort Array function. Then find the sum of each element in array.
Java:
// Function to Sort Array -- takes in array and its length
    private static int[] sortArray(int[] arr, int arrLength) {
        for (int i = 1; i < arrLength; i++) {
            int j = i;
            int temp = arr[j];
            while ((j > 0) && (arr[j - 1] < temp)) {
                // if arr[j-1] prev < arr[j] current -- swap
                arr[j] = arr[j - 1];
                j--;
            }
            arr[j] = temp;
        }
        return arr;
    }

  // Function to Generate An Array of 10 Numbers
    private int[] generateArray() {
        int[] x = new int[10];
        // Generate 10  Random numbers -- Random r = new Random();
        int low = 1;
        int high = 100;
        for (int i = 0; i < x.length; i++) {
            int result = (int) (Math.random() * (high - low + 1) + low);    
               for (int element : x) {
                    // Check for duplicate
                    if (element == result) {
                        // System.out.print("\t\t"+element+" <==> "+result);
                        break;
                    } else {
                        x[i] = result;
                    }
            }

        }
        // Reverse Sort -- Arrays.sort(x, Collections.reverseOrder());
        return sortArray(x, x.length);
    }

 // Array holding ten(10) winning numbers
        int[] winningStack = generateArray();
      
// Display Lucky Numbers
        System.out.println("\nLucky Numbers: \n");
        for (int element : winningStack) {
            if (element < 10) {
                System.out.print("\t\t" + "0" + element + "\t ");
            } else
                System.out.print("\t\t" + element + "\t");
        }       

// Calc the Sum of Winning Nos
int sum = 0;
for (int val : winningStack) {
    sum += val;
}

Question #2 . Bonus Game to generate Four(4) prize Sums . Solved by creating an Array using certain conditions.
My Prize Sums where $250, $500, $750, and $1000
Java:
 // Function to generate prize sums
    private int[] generatePrizeSum() {
        int[] prizeSums = new int[4];
        // Generate 4 prize sums
        int low = 1;
        int high = 100;
        int prizeSum = 0;
        for (int i = 0; i < prizeSums.length; i++) {
            int prizeNumber = (int) (Math.random() * (high - low + 1) + low);
            if (prizeNumber >= 0 && prizeNumber <= 25) {
                prizeSum = 250;
            } else if (prizeNumber <= 50) {
                prizeSum = 500;
            } else if (prizeNumber <= 75) {
                prizeSum = 750;
            } else if (prizeNumber <= 100) {
                prizeSum = 1000;
            }
            prizeSums[i] = prizeSum;
        }
        return prizeSums;
    }

Question #3. The User is also to Guess five(5) out of the ten(10) lucky numbers. Then the total Sum of all ten numbers.
Java:
System.out.println("\n\nStart Guessing ...  Five(5) of the Lucky Numbers ...... ");
        int guess;
        for (int i = 0; i < 5; i++) {
            System.out.print("Enter Number Guess : ");
            guess = scanner.nextInt();
            scanner.nextLine();
            if (i == 0) {
                // index (0) element
                userGuess[i] = guess;
                
            }
            if (i == 1) {
                // if guess is equals to index(0) element
                if (guess == userGuess[i - 1]) {
                    System.out.print("Number  already entered\n");
                    System.out.print("Enter Number Guess : ");
                    guess = scanner.nextInt();
                    scanner.nextLine();
                }
                userGuess[i] = guess;
              
            }
            if (i == 2) {
                // if guess is equals to index(1) element or index(0) element
                if ((guess == userGuess[i - 1]) || (guess == userGuess[i - 2])) {
                    System.out.print("Number  already entered\n");
                    System.out.print("Enter Number Guess : ");
                    guess = scanner.nextInt();
                    scanner.nextLine();
                }
                userGuess[i] = guess;
                
            }
            if (i == 3) {
                // if guess is equals to index(2) element or index(1) element or index(0) element
                if (guess == userGuess[i - 1] || guess == userGuess[i - 2] || guess == userGuess[i - 3]) {
                    System.out.print("Number  already entered\n");
                    System.out.print("Enter Number Guess : ");
                    guess = scanner.nextInt();
                    scanner.nextLine();
                }
                userGuess[i] = guess;
            }
            if (i == 4) {
                // if guess is equals to index(3) element or index(2) element or index(1) element or index(0) element
                if (guess == userGuess[i - 1] || guess == userGuess[i - 2] || guess == userGuess[i - 3] || guess == userGuess[i - 4]) {
                    System.out.print("Number  already entered\n");
                    System.out.print("Enter Number Guess : ");
                    guess = scanner.nextInt();
                    scanner.nextLine();
                }
                userGuess[i] = guess;
            }
        }

        System.out.print("Guess The Sum Total  of all ten(10) Lucky Nos:  ");
        int sumGuess = scanner.nextInt();

Question #4 and #5 and #6. Analyse How much the User earns
Java:
// Check for winning numbers in UserGuess
        int count = 0;

        // Array to hold the found lucky numbers
        int[] foundNumbers = new int[5];
        for (int number : winningStack) {
            for (int j = 0; j < userGuess.length; j++) {
                if (userGuess[j] == number) {
                    // count of found nos
                    count += 1;
                    foundNumbers[j] = userGuess[j];
                }
            }
        }

     int gamePoint = 0;
        switch (count) {
            case 1 -> gamePoint += 1000;
            case 2 -> gamePoint += 6000;
            case 3 -> gamePoint += 25000;
            case 4 -> gamePoint += 100000;
            case 5 -> gamePoint += 800000;
            default -> gamePoint += 0;
        }


        if (count > 0) {
            // Display the Found Numbers
            System.out.println("\n\nCorrect Guesses\n");

            for (int element : foundNumbers) {
                if (element == 0) {
                    System.out.print("\t\t" + "--" + "\t ");
                } else if (element < 10) {
                    System.out.print("\t\t" + "0" + element + "\t ");
                } else
                    System.out.print("\t\t" + element + "\t");
            }
            System.out.printf(":  => %d out of 5 Correct\n", count);

        } else {
            System.out.println(": => No Correct Guesses");
        }
        int winningAmount = 0;
        if (sum == sumGuess) {
            System.out.println("\n You guessed the sum Correctly: ");
            int sumPoints = 300000;
            // Pick higher if win both games
            if (count > 0) {
                winningAmount = Math.max(gamePoint, sumPoints);
                System.out.println("You won both Sum Guess & Lucky Number Guess");
                System.out.println("\n\nPrize Money Won: $" + winningAmount);
            } else {
                // Player got only Sum guess
                winningAmount = sumPoints;
                System.out.println("You won only SumGuess");
                System.out.println("\n\nPrize Money Won: $" + winningAmount);
            }
            System.out.println("..............For getting Sum Right.............");
            System.out.println("\nBonus Round : Prize Sums that can be won\n");
            System.out.println(" \t\t  $250   \t\t   $500  \t\t  $750  \t\t  $1000\n");
            int bonusPoints = bonusPrize(generatePrizeSum());
            System.out.println("Bonus Earned: $" + bonusPoints);

            System.out.println("\n\nTotal Money Won: $" + (winningAmount + bonusPoints));
        } else if (gamePoint != 0) {
            // Player got only Number Guess
            winningAmount = gamePoint;
            System.out.println("\nYou won Lucky Number Guess");
            System.out.println("\n You lost  Sum guess ...");
            System.out.println("\n\nPrize Money Won: $" + winningAmount);

        } else {
            // Player got none right
            System.out.println("\nNot Your Lucky Day ... Guessed wrong for both lucky numbers and Sum ");
            System.out.println("\nNo Prize Money Won");
        }
 
Joined
Mar 28, 2022
Messages
82
Reaction score
11
Here's another approach to answering question #1:

Java:
import java.util.ArrayList;
import java.util.Collections;

public class App {
  public static void main(String[] args) throws Exception {

    // populate a deck of possible values
    ArrayList<Integer> possibleValues = new ArrayList<Integer>();

    for(int i = 1; i <= 99; i++){
      possibleValues.add(i);
    }

    // shuffle the deck
    Collections.shuffle(possibleValues);
 
    // draw first ten items from the deck
    String output = new String();
    int sum = 0;

    for(int i = 1; i <= 10; i++){
      output += String.format("%02d  ", possibleValues.get(i));
      sum += possibleValues.get(i);
    }
    System.out.println(output + "\n" + sum);
 
  } // main
} // App
 
Last edited:
Joined
Mar 17, 2022
Messages
2
Reaction score
1
Thanks for the Solution. I was looking for a more traditional method for implementation without the use of ArrayLists, that's why I used array.
 
Joined
Mar 28, 2022
Messages
82
Reaction score
11
Same solution, with the newly added requirement of not importing java.util.Collections & java.util.ArrayList
Java:
import java.util.Random;

public class App {

  public static void main(String[] args) throws Exception {

    // populate a deck of possible values
    // ArrayList<Integer> possibleValues = new ArrayList<Integer>();
    int[] possibleValues = new int[99];

    for(int i = 0; i < 99; i++){
      possibleValues[i] = i+1;
    }

    // shuffle the deck
    Random rng = new Random();

    for (int i=0; i<possibleValues.length; i++) {
      int randomIndex = rng.nextInt(possibleValues.length);
      int temp = possibleValues[i];
      possibleValues[i] = possibleValues[randomIndex];
    }
 
    // draw first ten items from the deck
    String output = new String();
    int sum = 0;
    for(int i = 0; i < 10; i++){
      output += String.format("%02d  ", possibleValues[i]);
      sum += possibleValues[i];
    }
    System.out.println(output + "\n" + sum);
 
  } // main
} // App
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top