how do I print this correctly in java? [duplicate] - java

This question already has answers here:
Java: sum of two integers being printed as concatenation of the two
(10 answers)
Closed 5 years ago.
This is my java code
public class exercise {
public static void main(String[] args) {
int x = 8;
int y = 4;
System.out.println("x + y = " + x + y);
System.out.println("x * y = " + x * y);
System.out.println("x + x + y =" + x + x + y);
double z = x / y;
System.out.println("z = " + z);
}
}
it's supposed to look like this:
x + y = 12
x * y = 32
x + x + y = 20
z = 2.0
but when I run it with eclipse this is the result I get:
x + y = 84
x * y = 32
x + x + y =884
z = 2.0
As you can see 8 + 4 definitely != 84
As well as 8 + 8 + 4 != 884
It looks like the eclipse typed the values 8 and 4 in the first line, and didn't add them together, the same thing with the third line it just typed 8 and 8 and 4, not adding them together.
Do you have any idea how to fix this problem?

You need to put brackets around arithmetic operation
System.out.println("x + y = " + (x+y));
System.out.println("x * y = " + (x*y));
System.out.println("x + x + y ="+( x+x+y));

Related

Java Unexped Outputs From Pre-Increment

I am trying to figure out why the following code gives two different results
I tried the followingL
int x = 4, y = 4;
System.out.println(x + --x);
System.out.println(--y + y);
And It outputs 7 6. From my knowledge, preincrement has a higher precendence than addition so it should decrease the value of x/y regardless of it's value in the expression but this is clearly not the case. Can anyone please explain this to me?
System.out.println(x + --x);
System.out.println(--y + y);
This can be re-written as:
x + --x => x + (x = x-1) => 4 + 3 = 7
--y + y => (y = y-1) + y (y here is already decreased in value) = 3 + 3 = 6

What is the order of multiple pre/post incrementations in one line equation in JAVA?

I have this piece of code in JAVA 8:
int x=3;
int y = --x + x++ + --x ;
System.out.print("y: " + y + " x: "+x);
As I understand it should be split like this:
y = 2 + x++ + --x; x = 2
y = 2 + 2 + --x; x = 2
y = 2 + 2 + 1; x = 1
y = 5; x = 2
And it prints y: 6 x: 2
What is the order of operation in here?
It's because of the way pre and post increments work, it gets evaluated like this:
1. y = --x + x++ + --x ;
2. y = 2 + (2)++ + --(3);
3. y = 2 + 2 + 2;
4. y = 6
After 2 decrements and 1 increment, x becomes 2.

Java println weird output

I'm getting a weird output from running this simple java program.
The output is: 0 4 2 -6
Why does the x++ print 0, it should be printing 4.
import java.util.*;
import java.io.*;
public class Java1 {
public static void main(String[] args) throws IOException {
int x = 4;
int y = -5;
System.out.println(x++ + " " + func(x++, y) + " " + --y);
}
public static int func(int work, int y) {
int z = work + y;
work++;
y++;
System.out.print(z + " ");
return z + work + y;
}
}
Okay, here is what's going on: First x++ is evaluated, returning 4 (which is later printed) and leaving x at 5. Then x++ is evaluated again, passing 5 to func. Then func is evaluated with 5 and -5 parameters. In here z is 0 (5 + (-5) = 0) which is then printed (BEFORE the println in the main method. func then returns 2 (0 + 6 + (-4)) which is also added to the string. Finally --y results in -6. Now the println in the main method prints its string (4 2 -6).
func(x++, y) is executed first, so 0 comes from System.out.print(z + " "); in func.
System.out.print(z + " ");
is executed before
System.out.println(x++ + " " + func(x++, y) + " " + --y);
So the 0 comes from z, not x.
I have mentioned the flow of points from 0 to 7 in the comments
public static void main(String[] args) throws IOException {
int x = 4;
int y = -5;
System.out.println(x++ + " " + func(x++, y) + " " + --y);
// thus 0]4 6]2 (value returned as z) 7] localvalue of --y as -6
}
//1] x++ makes x as 5 when it is passed to func()
public static int func(int work, int y) {
int z = work + y;
//2] z = 5 + -5 = 0
work++;
//3] work which was x as 5 is now 6
y++;
//4] y will be -4 now
System.out.print(z + " ");
return z + work + y;
//5] z = 0 + 6 + -4 = 2 and is returned to func() caller
}
import java.util.*;
import java.io.IOException;
public class Java1
{
public static void main(String args[])
{
int x = 4;
int y = -5;
System.out.println("x = "+ (x++ ) +" func = "+ (func(x++, y) ) + " y = "+ --y);
}
public static int func(int work, int y)
{
int z = work + y;// 5+-5 = 0
work++; //6
y++; //-4
System.out.print("Z = " + z + " ");//0
return z + work + y; //0 + 6+-4 = 2
}
}
OUTPUT :
Z = 0 x = 4 func = 2 y = -6
Here the func() is executed first and hence the value of variable z is printed as 0 and then the x++ value is printed as 4.

JAVA println gives unexpected output

I have problem with JAVA code, that is I have a code:
class test {
public static void main(String[] args){
int x = 0;
int y = 0;
while( x < 5 ){
y = y - x;
System.out.print(x + "" + y + " ");
x += 1;
}
}
}
and I compile it in Command Prompt and I get this:
00 1-1 2-3 3-6 4-10
Where I expected output to be:
00 11 23 36 210
I don't know where the problem is.
Thanks for any responses.
The - in y = y - x; means subtraction.
Most likely you intended
y = y + x;
or
y += x;
Also most likely you expected.
00 11 23 36 410
the hyphen you see is not a hyphen but rather minus sign as y eventually becomes negative (based on ur code)
one way of doing it is by changing printout to be a positive value:
System.out.print(x + "" + y + " ");
to:
System.out.print(x + "" + Math.abs(y) + " ");
Your y is producing negative values:
Your code:
int x = 0;
int y = 0;
while( x < 5 ){
y = y - x;
System.out.print(x + "" + y + " ");
x += 1;
}
Let's try to debug it:
first iteration y = 0-0 = 0
second iteration y = 0-1 = -1
third iteration y = -1-2 = -3
fourth iteration y = -3-3 = -6
fifth iteration y = -6-4 = -10
As you can see, you're basically going opposite to your expected result which is a good indication that you're using an opposite operator on some variable, in this case y-x should be y+x.
Change y = y-x to y = y+x

Equations in Java - output is either 0 or incorrect value [duplicate]

This question already has answers here:
Division of integers in Java [duplicate]
(7 answers)
Closed 8 years ago.
I am new to Java and am trying to make a program run to solve and display
the output of the equations. I have tried changing the places of the
parenthesis, and putting equations in other parts of the code but I either get 0
as the output or wrong answers. Any words of wisdom would be greatly
appreciated.
import java.util.*;
import static java.lang.Math.*;
public class JFirstTest
{
public static void main(String[] args)
{
// Declare variables and equations
int W, X, Y, Z;
W = 10;
X = 20;
Y = 30;
Z = 40;
int FormulaOne = (W + X) / (Y + Z);
int FormulaTwo = (X + ( Y / W)) / Z;
int FormulaThree = X * (W - Y)/ (X * Y - Z);
// System.out.println formulas
System.out.println("\n\tWhen W=" + W + " X=" + X + " Y=" + Y + " Z=" + Z + " then FormulaOne = " + FormulaOne);
System.out.println("\n\tWhen W=" + W + " X=" + X + " Y=" + Y + " Z=" + Z + " then FormulaTwo = " + FormulaTwo);
System.out.println("\n\tWhen W=" + W + " X=" + X + " Y=" + Y + " Z=" + Z + " then FormulaThree = " + FormulaThree);
System.out.println("\n");
Update: This has been marked as Duplicate, however, I did searches before I
posted this question and the answers eluded me...Also changed int to double
and now it works fine.
Dividing integer by integer will result in integer, and round down (truncate). So, dividing smaller number by larger will result in zero, so you may want to consider changing type of variables to float / double.
Think of it this way... if an int (integer) is a whole number, then if you divide, for example, 10 by 20, then your program is just going to give you back 0 because 0.5 is not an integer (so it rounds down).
What primitive data type would you use to account for any decimals?
Answer: Double

Categories

Resources