-1

I am writing a programming project. My project is about the user entering money to buy the amount of chocolate bars they want to consume. Each chocolate bar contains 1 coupon. 1 chocolate bar equals $1. redeeming 6 coupons will get the user 1 free bar plus the extra coupon in the free bar. For example if the user enters 6$ the user will get 6 bars redeeming 6 coupons will get a free bar so the free bar will have a coupon so the the user has one coupon left.I must print the left amount of coupons after the user redeems it.

I use this equation to get the leftover coupons

amount = coupons / 6;

When i run my code the amount prints 0 not the remaining leftovers like 1 coupon left if i buy 6 chocolate bars. Please help what am I doing wrong. Heres my code

import java.util.Scanner;
public class Chp4PP16RedeemChocolateCoupons_John {

public static void main(String[] args) 
{
    //Instance Variables
    int CostOfBar = 1;
    int ChocolateBar = 0;
    int UserDollar = 0;
    int Amount;
    int Coupons = 0;
    int Total = 0;

    Scanner keyboard = new Scanner(System.in);
    //Tell the user how many chocolate bars they want to buy
    System.out.println("Welcome User,\nPlease enter your money to buy the amount of chocolate bars you want.\nChocolate Bar = $" + CostOfBar);
    UserDollar = keyboard.nextInt();

    System.out.println("You have entered $" + UserDollar+ "\n");
    System.out.println("You have decided to buy");
    System.out.println((Amount = UserDollar + ChocolateBar) + " Chocolate Bars.");


    //get total amount and remainder of coupons
    while (Coupons > 6)
    {
        Total = Amount / 6;
    }
    System.out.println("\nYou have " + Total + " remaining coupons.");


}

}

TollesonC
  • 9
  • 4
  • 1
    (1) The `while` loop would be an infinite loop if `coupons` is greater than 6. Not sure why you're using `while` here. (2) In this case, it doesn't even enter the `while` loop since `coupons` = 6, not greater. – devnull May 02 '14 at 02:09
  • 1
    Unless you do something to `coupons` in the `while` loop, it'll go on and on once it's within. And, please, do not use title case variable names. – devnull May 02 '14 at 02:11
  • Also, you need to be careful with `amount = coupons / 6`. If `amount` is the leftover coupons as you say, then what happens if `coupons` is 5? – awksp May 02 '14 at 02:20
  • 1
    idiomatic Java has Object names as `UpperCamelCase` and variable names as `lowerCamelCase`, not following this rule is very off putting to seasoned Java programmers and makes code hard to read. Here specifically the syntax highlighter isn't that smart and sees `CostOfBar` as a Class name and not a variable name and colors it incorrectly. Same with the rest of them. –  May 02 '14 at 03:04

1 Answers1

0

As mentioned in a comment on your post, your while loop will be infinite as the condition is that ChocolateBars > 6, which will never be false. Additionally, the code inside the loop will keep dividing by 6, which is not what you want at all. I think you need to learn about modulus division.

This should work for you:

Coupons = ChocolateBar;
//now Coupons contains the total amount of coupons the user will have gained
 do {
    Total = Coupons % 6; //Here we use modulo to make the total left be the remainder of the amount of coupons divided by 6        
    Total += Coupons / 6; //Add on the amount of coupons redeemed in the free chocolate bars 
    Coupons = Total;
} while (Coupons >= 6);
System.out.println("\nYou have " + Total + " remaining coupons.");

Edit: Also realised you need to fix your calculation of how many chocolates to buy. This line needs to change:

System.out.println((Amount = UserDollar + ChocolateBar) + " Chocolate Bars.");

to something like this:

ChocolateBar = UserDollar/CostOfBar;
System.out.println(ChocolateBar + " Chocolate Bars.");
Community
  • 1
  • 1
SharkofMirkwood
  • 11,483
  • 2
  • 17
  • 25
  • 1
    I do not understand your for loop. Surely this code is the same as `ChocolateBar = UserDollar/CostOfBar; Coupons = ChocolateBar` – Scary Wombat May 02 '14 at 02:46