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...
Related
for(i = 0; i<=timePassed; i++) {
y = r * x * (1 - x)
System.out.println(y)
So how do I reset the value of y every time the for loop loops so that i can assign a new value to it? I cannot wrap my head around it I have already tried to set y to 0 at the end of the loop right after printing y but that didnt seem to work. I also tried setting it to null but that gave me an error(?) I cannot understand why this is happening can someone please explain how to do this and what I did wrong?
Are you able to update your code block to include ALL code from your class.java file? It would help us see what could be wrong, because from this code snippet, y is not saved outside of the loop, therefore for every iteration (cycle) of the loop it would be reset and re-assigned.
As someone else has said, the y value does not change at any time in the loop, as all variables used (other than y) are declared outside of the loop. This loop simply prints the same y value "timePassed" amount of times.
Here's the code that you posted, including line numbers:
1. for(i = 0; i<=timePassed; i++) {
2. y = r * x * (1 - x)
3. System.out.println(y)
4.
Here are various comments + fixes:
Line 1: assume i is declared somewhere, like this: int i
Line 1: assume timePassed is declared somewhere and has a value set, like: int timePassed = 5;
Line 2: assume y is declared somewhere, like: int y;
Line 2: assume r and x are both declared somewhere, and have values set, like: int r = 1, x = 2;
Lines 2-3: need semicolons to terminate each line inside the loop: add ; at the end of each line
Line 4: add closing brace } to the "for" loop
Here is the code with those edits applied:
int timePassed = 5, y, r = 1, x = 2;
for (int i = 0; i <= timePassed; i++) {
y = r * x * (1 - x);
System.out.println(y);
}
Running that code produces this output:
-2
-2
-2
-2
-2
-2
The output doesn't change. Why? Because the calculation to determine each new value of "y" is the same each time: r * x * (1 - x). Despite being inside a loop, and with i changing each iteration through the loop, i is not used in the calculation at all.
Here's an edit to the code, removing the entire "for" loop (and i), and it produces the same -2 value:
int y, r = 1, x = 2;
y = r * x * (1 - x);
System.out.println(y);
-2
To demonstrate that y is being set each time through the loop, here's a variation of the code (including output) where i is included in the calculation for y.
int timePassed = 5, y, r = 1, x = 2;
for (int i = 0; i <= timePassed; i++) {
y = r * x * (1 - x) * i;
System.out.println(y);
}
0
-2
-4
-6
-8
-10
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
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
Below is come code that I am having trouble understanding. The output is 13 15 x=6. I understand how we get the number 13 because when we go through the loop the value of x is 5 and its corresponding y value is 12. So if x > 4 we then increment y which gives u the value of 13. The next number printed out then should be 14 because the next value of x is 6 and its corresponding y value is 13 so when you increment that it will be 14. However when I run the code its gives a different answer. Can anyone please help? Thanks
public class Output {
public static void main(String[] args) {
Output o = new Output();
o.go();
}
void go() {
int y = 7;
for (int x = 1; x < 8; x++) {
y++;
if (x > 4) {
System.out.print(++y + " ");
}
if (y > 14) {
System.out.println(" x = " + x);
break;
}
}
}
}
The reason is because ++y increments y before printing it.
y++ would do what you are expecting
When entering the loop iteration where x is 6, y is 13, as you said. Then y is immediately incremented to 14. x is greater than 4, so System.out.print(++y + " "); is executed. ++y increments y to 15 before it is printed.
public class Static
{
static
{
int x = 5;
}
static int x,y;
public static void main(String args[])
{
x--; myMethod();
System.out.println(x + y + ++x);
}
public static void myMethod()
{
y = x++ + ++x;
}
}
Could you please somebody help me here why it is displaying out put is 3?
static
{
int x = 5;
}
You redeclare x here, making it a locally scoped variable (not a class member). This assignment will have no affect whatsoever, regardless of when it is run.
Now, you asked about the static block and that's what I answered. If you are confused about why a value of 3 is outputted even assuming that assignment doesn't take place, then this becomes a question about the increment operators (x++ and ++x).
Full explanation
I like Paulo's explanation quite a bit but let's just see if we can simplify the code. To start, let's forget about making x and y a static field (making them local, initialized to the default for a static int: 0) and inline myMethod():
int x = 0, y = 0;
x--;
y = x++ + ++x;
System.out.println(x + y + ++x);
First we should eliminate complex expressions. We can do that by extracting each sub-expression into a temporary variable in the correct order (expressions are evaluated left-to-right):
int x = 0, y = 0;
x--;
int yOperand1 = x++;
int yOperand2 = ++x;
y = yOperand1 + yOperand2;
int resultOperand1 = x;
int resultOperand2 = y;
int resultOperand3 = ++x;
int result = resultOperand1 + resultOperand2 + resultOperand3;
System.out.println(result);
Now we can label the value of x, y and any temporary variables at each step:
int x = 0, y = 0; //x: 0 y: 0
x--; //x: -1 y: 0
int yOperand1 = x++; //x: 0 y: 0 yOperand1: -1
int yOperand2 = ++x; //x: 1 y: 0 yOperand1: -1 yOperand2: 1
y = yOperand1 + yOperand2; //x: 1 y: 0
int resultOperand1 = x; //x: 1 y: 0 resultOperand1: 1
int resultOperand2 = y; //x: 1 resultOperand1: 1 resultOperand2: 0
int resultOperand3 = ++x; //x: 2 resultOperand1: 1 resultOperand2: 0 resultOperand3: 2
int result = resultOperand1 + resultOperand2 + resultOperand3; //result: 3
System.out.println(result);
Your static block has a local variable named x, it does not change the static variable with the same name.
In general, static { ... } blocks will be merged by the compiler with the initializers of static variables in the order they are in the code, and executed on class initialization. (In bytecode, this is a static <clinit> method.)
In your program the following occurs:
the static variables x and y are initialized with their default values (0).
the static block with its local variable is executed, this has no effect on the rest of the program.
the main method will execute.
it sets x = -1.
it invokes myMethod().
myMethod will twice increment x (from -1 to 0 to 1), and set y to 0 (the value before the first incrementing + the value after the second one).
it adds x (1), y (0) and the value after increasing x again (2), and prints the result (3).
Static.x starts with the value 0 since {int x = 5;} creates a new variable that is only visible within the static block. Since that x is never used it does nothing. If you removed it you should be able to trace through and see why your answer is what it is. :)
What you want is either to make the contents of the static block an assignment rather than a declaration ( ie static{ x = 5; }) or to assign x the value 5 at declaration static int x = 5;.