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.
Related
I am currently trying to create a do-while loop, counting from 1 to 10, that displays the first five numbers on the first line and the next five numbers on another line.
But whenever I run my code, the 6th iteration and onward print on seperate lines each instead of the same line.
If anyone could help me understand the error that I made and how to corret it, I would appreciate it.
import java.text.DecimalFormat;
import java.util.Scanner;
public class Hello_World { // Declare Class
public static void main(String[] args) { // Main Method
Scanner key = new Scanner(System.in);
DecimalFormat df = new DecimalFormat("##.##"); // Decimal_Format!
/*
* While Loop count from 1 to 10
*/
int x = 1; // Declare and Initialize variable 'X'
do
{
System.out.print(x + " ");
x++;
if (x > 5) {
System.out.println("");
continue;
}
}
while (x <= 10);
}
} // Braces delimit blocks of code!
You only want to print the newline when x is 6. Also, you could use printf to make this a bit cleaner. Something like,
int x = 1; // Declare and Initialize variable 'X'
do {
if (x == 6) {
System.out.println();
}
System.out.printf("%3d ", x);
x++;
} while (x <= 10);
Outputs
1 2 3 4 5
6 7 8 9 10
The error in your code is with your if-statement
By setting the if statement to be true if x > 5 it will trigger for 6 through 10 which is why you get your numbers above 5 all on a separate line. If you just set the new line to kick in when x == 6 it will only trigger a new line when x is 6 and in no other circumstance.
I prefer a two-loop solution. The outer loop increments through the entire range you want; the inner loop increments through a single line.
int limit = 10;
int perLine = 5;
for (int x = 1; x <= limit; x += perLine) {
for (int y = x; y < x + perLine && y <= limit; y++) {
System.out.printf("%3d", y);
}
System.out.println();
}
This solution works even if the 'limit' is not an exact multiple of 'perLine'.
I wrote this with a for-loop out of sheer habit, but you could convert it to do-while easily enough.
int limit = 10;
int perLine = 5;
int x = 1;
do {
int y = x;
do {
System.out.printf("%3d", y);
y++;
} while (y < x + perLine && y <= limit);
System.out.println();
x += perLine;
} while (x <= limit);
However, it's a lot more long-winded that way, so unless this is being done as an exercise in using do-while, I'd use a for-loop.
while loop is not working in the if statement, why its not working... and how can while loop can work in if curly braces.
public static void main(String[] args) {
int x = 30;
if (x < 20) {
System.out.print("This is if statement");
int a = 10;
while(a < 20) {
System.out.print("value of x : " + a );
a++;
System.out.print("\n");
}
} else {
System.out.print("This is else statement");
}
}
You declared x as 30, the if loop executes if(x<20) which is always false, so its performing the else statement immediately without going thru the if statement.
You either have to declare x for something less than 20 or change the if statement.
Your if statement doesn't meet the condition. Try this:
public static void main(String[] args) {
// TODO code application logic here
int x = 10;
if( x < 20 ) {
System.out.print("This is if statement");
int a = 10;
while( a < 20 ) {
System.out.print("value of x : " + a );
a++;
System.out.print("\n");
}
}else {
System.out.print("This is else statement");
}
}
And output:
This is if statementvalue of x : 10
value of x : 11
value of x : 12
value of x : 13
value of x : 14
value of x : 15
value of x : 16
value of x : 17
value of x : 18
value of x : 19
you assigned the value for x is 30 , but in your condition you are checking x is less than 20 , hence it the condition will fail and it wont execute...
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
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...
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