Home > Enterprise >  Unable to add 20 "points" for reward program for above 3 beverages
Unable to add 20 "points" for reward program for above 3 beverages

Time:02-26

I'm trying to add 20 "points" for every beverage (or drink) that a customer had the previous week for a rewards program after 3 beverages. However, I can't seem to add 20 points for every beverage past 3. It just adds 20 additional points, and then stops adding after that.

For example, if a customer had 4 beverages last week, then they should have 50 reward points; 70 reward points for 5 beverages, 90 points for 6 beverages, and so on. What my program is currently doing (or has done) is gone to 50 or 70 and then stayed the same (it's at 50 currently), no matter the number inserted, it's gone negative and subtracts 20, or infinitely loops without a break.

The numbers 1-3 are already taken for having 5 points, 15 points, and 30 points, respectively.

import java.util.Scanner;

public class RedDevilCoffeeRewards 
{
    public static void main(String[] args) 
    {
        // Create Scanner
        Scanner scanner = new Scanner(System.in);

        // Ask user how many beverages (or drinks) they bought last week
        System.out.println("How many beverages did you purchase last week");

        // Allows user input for amount of beverages bought last week
        int beverages = scanner.nextInt();
        int beverages4 = 50;
        int beveragesaddpoints = 20;
        int beveragepointtotal = beverages4   beveragesaddpoints;
        //int beveragesmorethan4= beverages4   20;

for (int beverages4orhigher = beveragepointtotal; beverages4 >= 4;   beverages4)
        {
            if (beverages >= 4)
            {
                beveragepointtotal  = beveragesaddpoints;
                System.out.println("You have earned "   beverages4   " points");
                break;
            }
        }

CodePudding user response:

The problem with your code is in the for loop that you have mentioned. You have declared a variable beverages4orhigher which is not used in the loop and then you are breaking out of the loop based on the beverages value which will be always be true in case the value is more than 4.

ii) You are printing the value of beverages4 and not beverageinTotal that should be printed.

You need to rectify these 2 changes.

   public class RedDevilCoffeeRewards {
    public static void main(String[] args) {
        // Create Scanner
        Scanner scanner = new Scanner(System.in);

        // Ask user how many beverages (or drinks) they bought last week
        System.out.println("How many beverages did you purchase last week");

        // Allows user input for amount of beverages bought last week
        int beverages = scanner.nextInt();
        int beverages4 = 50;
        int beveragesaddpoints = 20;
        int beveragepointtotal = beverages4   beveragesaddpoints;
        // int beveragesmorethan4= beverages4   20;
        int points = 0;
        if (beverages == 1) {
            points = 5;
        } else if (beverages == 2) {
            points = 15;
        } else if (beverages == 3) {
            points = 30;
        } else {
            points = 30;
            beveragepointtotal = beverages - 3;
            for (int i = 0; i < beveragepointtotal; i  ) {
                points  = 20;
            }
        }

        System.out.println("You have earned "   points   " points");
    }
}

and the sample output is

How many beverages did you purchase last week
6
You have earned 90 points
  •  Tags:  
  • java
  • Related