Reputation: 13
For some reason I am unable to get the following code to work like it should. This is what I have in java:
int num = 8;
System.out.println("Value is now " + (num));
System.out.println("Value is now " + (num+2));
System.out.println("Value is now " + (num-4));
System.out.println("Value is now " + (num*5));
System.out.println("Value is now " + (num/3));
System.out.println("Value is now " + (++num));
System.out.println("Value is now " + (--num));
It prints the following:
Value is now 8
Value is now 10
Value is now 4
Value is now 40
Value is now 2
Value is now 9
Value is now 8
I was expecting to get this:
Value is now 8
Value is now 10
Value is now 6
Value is now 30
Value is now 10
Value is now 11
Value is now 10
So, how do I get the answer to carry down from line to line? I have been reading and searching and cannot find the right way.
Upvotes: 1
Views: 309
Reputation: 3
You need to assign the new value.
System.out.println("Value is now " + (num));
System.out.println("Value is now " + (num+=2));
System.out.println("Value is now " + (num-=4));
System.out.println("Value is now " + (num*=5));
System.out.println("Value is now " + (num/=3));
System.out.println("Value is now " + (++num));
System.out.println("Value is now " + (--num));
System.out.println
is there to print out all the variables you need to the console.
Upvotes: 0
Reputation: 67
A common mistake! You're using your variables inline, which means that the variables themselves will not be changed. In order to actually change the variables, you need to use '='. This will change the actual value of your variable.
So, for example,
int num = 8;
System.out.println("Value is now " + (num));
num = num + 2;
System.out.println("Value is now " + (num));
num = num - 4;
System.out.println("Value is now " + (num));
num = num * 5;
System.out.println("Value is now " + (num));
num = num / 3;
System.out.println("Value is now " + (num));
System.out.println("Value is now " + (++num));
System.out.println("Value is now " + (--num));
Now this is a lot of code. Java provides some nice little features that can shorten this, called "Syntactic Sugar." First, the '=' will return the value set to the variable. So now we can write it like this:
int num = 8;
System.out.println("Value is now " + (num));
System.out.println("Value is now " + (num = num + 2));
System.out.println("Value is now " + (num = num - 4));
System.out.println("Value is now " + (num = num * 5));
System.out.println("Value is now " + (num = num / 3));
System.out.println("Value is now " + (++num));
System.out.println("Value is now " + (--num));
Next, we can write these operations as shorthand. num = num + 2
becomes num+=2
, num = num * 5
becomes num*=5
, and so on.
So, we can write the entire code as:
int num = 8;
System.out.println("Value is now " + (num));
System.out.println("Value is now " + (num += 2));
System.out.println("Value is now " + (num -= 4));
System.out.println("Value is now " + (num *= 5));
System.out.println("Value is now " + (num /= 3));
System.out.println("Value is now " + (++num));
System.out.println("Value is now " + (--num));
Have a nice day!
Upvotes: 4
Reputation: 67320
In Java, primitives (eg: int
) are "immutable". This means you can't directly change them (AKA mutate them), you can only create new ones.
If you want to effectively modify the int
num
, you have to reassign to it. This will overwrite the current value with the new one. Here is an example:
int num = 8;
System.out.println("Value is now " + num);
num = num + 2;
System.out.println("Value is now " + num);
num = num + 2;
But perhaps a better way to visualize this is to think of it like this:
int num1 = 8;
System.out.println("Value is now " + num1);
int num2 = num1 + 2;
System.out.println("Value is now " + num2);
int num3 = num2 + 2;
I say this is a better way to visualize it because in the original example, you aren't changing num
, you are just reassigning it. The second example is making this more explicit.
Upvotes: 0
Reputation: 1
Other than your last two lines, you're simply giving the println the value of your variable when the math is applied to it. The last two lines work, because ++x/--x represent x = x+1 or minus. You would need to assign your variable in this fashion for your code to work as you specified.
Upvotes: 0
Reputation: 176
The way you are doing it right now is not changing the value of the variable.
You can read num-4
as "what will be the number if I subtract 4 from num".
You are looking for something like num = num - 4
this is "assign to num new value that is equal to 4 substracted from num".
Upvotes: 0
Reputation: 2387
Just assign the new value to your variable.
int num =8
System.out.println("Value is now " + num);
num = num +2;
System.out.println("Value is now " + num);
num = num -4;
System.out.println("Value is now " + num);
num = num *5;
System.out.println("Value is now " + num);
......
Upvotes: 1
Reputation: 16691
The problem you're having is that you're not actually changing the value of num. Num is always 8, and so you are just printing out various operations on the number 8. Why don't you try something like this:
int num = 8;
System.out.println("Value is now " + (num));
num += 2;
System.out.println("Value is now " + (num));
num -= 4;
System.out.println("Value is now " + (num));
num *= 5;
System.out.println("Value is now " + (num));
num /= 3;
System.out.println("Value is now " + (num));
num++;
System.out.println("Value is now " + (num));
num--;
System.out.println("Value is now " + (num));
EDIT
Ran in command line, received expected output:
Value is now 8
Value is now 10
Value is now 6
Value is now 30
Value is now 10
Value is now 11
Value is now 10
Upvotes: 1