FAQs on the exercise Compound Assignment Operators
There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
You can also find further discussion and get answers to your questions over in Language Help.
Agree with a comment or answer? Like () to up-vote the contribution!
Hey. So I have written this:
public class BakeSale {
public static void main(String args) {
int numCookies = 17;
int numCupcakes = 12;
// Now cookies are being sold. First I sell 3, then I sell half.
int numCookies -= 3;
int numCookies /= 2;
// Let's see how many is left'
System.out.println(numCookies);
}
}
This produce errors:
BakeSale.java:6: error: ‘;’ expected
int numCookies -= 3;
^
BakeSale.java:7: error: ‘;’ expected
int numCookies /= 2;
^
2 errors
I think I’ve written everything just like the assignment explains. I even passed the assignment and can continue.
Once we declare the data type of a variable, we don’t need to do it again. Because you already indicated that numCookies and numCupcakes are ints, you should not do it again when you are manipulating them. What change do you need to make?
I feel strange about how this exercice is turned…
The class is named BakeSale so we can assume this class is about counting the sales of the bakery and we’re told that a customer bought 3 cookies so it’s 3 more cookies sold and the best would be to add those to the number of cookies sold.
I don’t really understand why you’ve turned it that way…
I can’t imagine that we don’t need anything to clarify the variable name. And since imagination is not the name of the game. It worked but I don’t understand.
public class BakeSale {
public static void main(String args) {
int numCookies = 17;
numCookies -= 3;
numCookies /= 2;
// Add your code above
System.out.println(numCookies);
we already specified that numCookies is an integer.
Once we have declared the type of the variable, we can neither declare it again nor change the data type.
int numCookies = 17;
int numCookies = 10; // This will give error because we have already
// declared numCookies earlier in the code.
int numCookies = 17;
double numCookies = 17; // This will also give error
int numCookies = 17;
numCookies = 10; // This is fine because we aren't declaring the variable again.
// We are just re-assigning a new value to the variable.
// The new value is of the same type as the type in our
// declaration, so there is no issue.
Thank you mtrtmk! It was just difficult to expect that this would work. I appreciate your clarification.
numCookies = 10; // This is fine because we aren’t declaring the variable again.
// We are just re-assigning a new value to the variable.
// The new value is of the same type as the type in our
// declaration, so there is no issue.