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
Related
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));
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.
my program and question are below
public class test {
public static void main(String[] args) {
int x = 0;
int y = 0;
while ( x < 5 ) {
y = x - y;
System.out.println(x + "" + y);
x = x+1;
}
}
}
So output for this is 00 11 21 32 42. I understand what happens when x is even but what when x is odd? Let's move to step 2 and make x=1, then we get
y=1-y
2y=1
y=1/2
For me output should be like 11/2 or sth like that
So how the hell output for this is 11? Do we use approximation? Thanks for answer.
No. You understand it wrong. You are doing math keeping the fact aside that evaluation of Java expression.
y = x - y;
means
y= 1-0;
Which is
y = 1
There's nothing off with your code, the result is correct. When x is 1, we set y to be equal to x - y. That means x and y are both 1, as 1 - 0 is 1. Remember that a = b in programming is an assignment and does not imply equality.
Isn't it obvious? By "=" you assigning new value into variable.
x = 0, y = 0
y = 0 - 0 = 0
print x y -> 0 0
x = 1, y = 0
y = 1 - 0 = 1
print x y -> 1 1
x = 2, y = 1
y = 2 - 1 = 1
print x y -> 2 1
etc...
public class NormalNumbers {
public static void main(String[] args) {
int x = 1;
while ((x >= 1) && (x <= 100)) {
System.out.println("x = " + x);
x = x + 1;
}
}
}
The current output is:
x = 1
x = 2
...
x = 100
I want to change the format to:
x=1 x=2 x=3 x=4 x=5
x=6 x=7 x=8 x=9 x=10
and so on.
How do I achieve that?
Instead of using println(), which automatically inserts a newline character at the end of whatever you're printing, just use print() and add an extra space to pad your entries.
If you want to inject a newline after 5 entries specifically, you can do so with an empty println() and the modulus operator like so:
while ((x >= 1) && (x <= 100)) {
System.out.print("x = " + x);
if (x % 5 == 0) {
System.out.println();
}
x = x + 1;
}
Divide your counter by 5 using modulus division if there is no remainder then create a new line:
int x = 1;
while ((x >= 1) && (x <= 100))
{
System.out.print("x = " + x + " ");
if(x % 5 == 0)
{
System.out.print("\n");
}
x = x + 1;
}
println is next line, print is on the same line.
x % 5 == 0 checks that the x values is a multiple of 5 or not.
int x = 1;
while ((x >= 1) && (x <= 100)) {
if (x % 5 == 0) {
System.out.println("x="+x);
} else {
System.out.print("x=" +x+ " ");
}
x = x + 1;
}
That gives you output as
x=1 x=2 x=3 x=4 x=5
x=6 x=7 x=8 x=9 x=10
x=11 x=12 x=13 x=14 x=15
x=16 x=17 x=18 x=19 x=20
-----
System.out.println prints the text and adds a new line. Use System.out.print to print on the same line instead.
So it would be something like this:
System.out.print("x=" + x + " ");
To add a new line each 5 numbers, use:
// if x is multiple of 5, add a new line
if (x % 5 == 0) {
System.out.println();
}
PD: You can use x++ (increment operator) or x += 1 (in the case you want to increase in more than one unit) instead of x = x + 1.
PD2 : You might want to use a tabulation (\t) instead of a space for separating your numbers. That way, numbers with two digits will have the same indentation than numbers with one digit.
System.out.print("x=" + x + "\t");
I think that in your case the better way is using for(;;) statement:
for (int x = 1; x > 0 && x < 101;)
System.out.print("x = " + x + (x++ % 5 == 0 ? "\n" : " "));
The ternary operator x++ % 5 == 0 ? "\n" : " " is responsible for the new line and increment the x variable.
Output:
x = 1 x = 2 x = 3 x = 4 x = 5
x = 6 x = 7 x = 8 x = 9 x = 10
...
x = 96 x = 97 x = 98 x = 99 x = 100
I have the following questions, about this Java Code:
public static void main(String[] args) {
int A = 12, B = 24;
int x = A, y = B;
while (x > 0) {
y = y + 1;
x = x - 1;
}
System.out.println("the solution is " + y);
}
What is beeing computed here?
My solution is, that it's (12-1)+(24+1) = 36. Please correct me if it's the wrong though.
For which A and B there will be an error?
Honestly, i though about A = 1 and smaller, but it didn't work....can someone help me out?
If there's an error, what is the readout?
I could not answer this, as trying out to get an error (for example setting A = -24) i just did not get an error but another solution.
Incrementing y, x times
There'll be no errors for any A and B. In the "worst" case, the loop won't be executed and the initial value of y will be printed
Irrelevant
I don't understand your title, there's nothing to do with String[] args here.
I'm unsure what's the purpose of this code, even for learning purposes..
Let's check this bit of your code:
while (x > 0) {
y = y + 1;
x = x - 1;
}
System.out.println("the solution is " + y);
This is a loop. A while loop, to be precise.
A loop will continue to iterate until the condition of the loop is false
In this case, you have x = 12 and y = 24. Since x value is positive, so it will enter the loop, and continue the calculation for each iterations.
Here are the results you'll get for each iteration:
x = 11, y = 25
x = 10, y = 26
x = 9, y = 27
x = 8, y = 28
x = 7, y = 29
x = 6, y = 30
x = 5, y = 31
x = 4, y = 32
x = 3, y = 33
x = 2, y = 34
x = 1, y = 35
x = 0, y = 36
When you get x = 0 and y = 36, the loop stops, because x = 0 and it violates the condition. So you get out of the loop. The last value of y is 36. That's what you're getting in you println(); it's not x + y, it's y alright.
But when x = -12, or x=-24 or any other negative number, the while condition is false. Your println() is outside the loop, so it will display the value of y which is outside the loop, i.e. 24.
You won't get an error for condition of while being false. Even if you println() x or y inside the loop, you won't get any result, but won't get any error either.
sum of A and B
when A<0 the sum is not calculated correctly
readout is B when there is error