I have been struggling with an exercise in the Java Headfirst book( CH5: p121 for reference). It's a loop inside another loop which adds/substracts some values from instance variables.
Input:
x = x + 3
Outputs:
x= 54 y = 6
public class MixFor5 {
public static void main(String[] args) {
int x = 0;
int y = 30;
for (int outer = 0; outer < 3; outer++) {
for (int inner = 4; inner > 1; inner--) {
x = x + 3;
y = y - 2;
if (x == 6) {
break;
}
x = x + 3;
}
y = y - 2;
}
System.out.println(x + " " + y);
}
}
My result is when doing it by myself with a notepad is x=42 y = 8 because then both loop conditions are met. What am i doing wrong? where did I go wrong in my thoughtprocess?
these are my notes -> pastebin note
I have not tried debugging first because I want to figure this by myself first so that I don't make the same mistakes in the future.
Thanks in advance,
tvanderv
if(x == 6) will never get true. The reason behind this is,
When inner = 4
x = x + 3 executes two times i.e. means x = 6.
then, inner = 3
now first x = x + 3 (before if(x == 3) condition) will give output x = 9. So x > 6 it will not break loop.
You did this step wrong in your notes.
Related
Say I have the following variables:
int x = 1;
int y = 2;
//some calculations follow(x and y stay the same init values) that somehow require you to interchange the values of y and x
how can I set y = 1 and x = 2 in one line of code??
Not sure why that's necessary, but you can do it like:
int x = 2, y = 1;
Try using bitwise XOR(^) operator.
x = x ^ y ^ (y = x);
Your completed code may look like,
class Main
{
public static void main (String[] args)
{
int x = 1, y = 2;
x = x ^ y ^ (y = x);
System.out.println("x after swapping:\nx="+x+"\ny after swapping,\ny="+y);
}
}
I need an explanation for the following loop:
public static void main(String[] args) {
int x = 0;
int y = 30;
for (int outer = 0; outer < 3; outer++) {
for (int inner = 4; inner > 1; inner--) {
x = x + 6;
y = y - 2;
if (x == 6) {
break;
}
x = x + 3;
}
y = y - 2;
}
System.out.println(x + " " + y);
}
The way I'm seeing it is that the 'outer' loop is running 3 times and the 'inner' loop is running 9 times. When I go through the 'inner' loop, x becomes 6, and we break out of the 'inner' loop after we get the value for y, which is 28. So now, the value of x is 6, and now I go through the 'outer' loop which runs 3 times, so 3 times 2 is equal to 6, so I subtract that from 28, and I end up with 22.
Output:
6 22
Does anyone know what I am doing wrong? I know the output should be 60 10, but I am not getting this. Thanks for your help.
I have copied and pasted your code in my pc and I get the out put
60 10
I think your are running a different file in your IDE.
What IDE do you use?
public class Main {
public static void main (String[] args) {
int x = 0;
int y = 0;
while (x < 5) {
y = x - y;
System.out.print(x + y);
x = x + 1;
}
}
}
When I calculate this math myself. I get these answers:
y = 0 - 0 = 0
y = 1 - 0 = 1
y = 2 - 1 = 1
y = 3 - 1 = 2
y = 4 - 2 = 2
01122
But when I compile it. I get the answer
02356
I just don't get it. Could someone explain?
You are printing x+y not y
0+0=0 ; 1+1=2; 1+2=3; 2+3=5; 2+4=6
02356
In first iteration x=0,y=0 so x-y = 0 = y and x+y=0 so 0 will be printed.
In 2nd iteration x=1,y=0 so x-y = 1 = y and x+y=2 so 2 will be printed.
thus x and y will be updated.
In your calculation, you are not updating y
Use the debug, you will see that the iterations are correct because you are manipulating the x value after the x+y operation addition
I know that the answer is 9, but when I try to work this out on paper, I keep getting that the answer is 6. I need to know what is happening with the code runs. Especially since in the beginning, the second for loop is false because x isn't less than y.
What value is printed when the above code is executed?
int w = 0;
for (int y = 0; y < 5; y++)
{
w = y - 1;
for (int x = 1; x < y; x++)
{
w = w + x;
}
}
System.out.println(w);
The only loop that matters is the last one since the first part of the loop sets w without any care about it's previous value: y = 4.
So w = 3.
Then a loop that adds 1 2 and 3 to w.
w = 3 + 1 -> w=4
w = 4 + 2 -> w = 6
w = 6 + 3 -> w = 9
The statement says:
Write a list of multiples of 7 between 98 and 266, both
including
I put this code:
import java.util.*;
public class Multiples7 {
public static void main (String[] args) {
Scanner entrada;
int x;
entrada = new Scanner(System.in);
while (x >= 98 && x <= 266) {
if (x % 7 == 0){
System.out.println(x);
}
}
}
}
and I get this error that I don't understand:
variable x might not have been initialized
Why x not start?
To solve the question asked: you simply need to initialize x, which is currently uninitialized. To initialize a variable, you have to assign it a value. For example x = 0;.
However, that still is not going to cause your program to print the correct result.
One way to accomplish what you actually want to do is iterate the numbers between 98 and 266 print them when they are divisible by 7.
for(int y = 98; y <= 266; ++y) if (y % 7 == 0) System.out.println(y);
alternately, you can start at 98 (14 * 7) and then increment it by 7, printing as you go.
int y = 98;
while(y <= 266) {
System.out.println(y);
y+=7
}
You need to read the value of x or initialize it yourself. This error is shown because there is a chance that the program might get over without x being initialized.
Just initialize it :
int x = 0;
or read from scanner
x = entrada.nextInt();
Alternatively, you could use a for loop, which includes initialization.
for (int x = 98; x <= 266; x++) {
if (x % 7 == 0) {
System.out.println(x);
}
}
You have only declared x but did not initialize it. Insted of int x do int x = 0;. Replace 0 with the desired value.
You need to give X a starting value or it might as well not exist.
For example if X should start at 0 then use:
int x = 0;
you need to initialize x so it has a starting value and is not empty when your programm starts(int x = 98;). Also you should increment x inside your while loop (x++; or you will have an infinity loop always printing the same line.
int x = 98;
entrada = new Scanner (System.in);
while ( x >= 98 && x <= 266) {
if (x % 7 == 0){
System.out.println(x);
}
x++;
}
It could be a single for loop. Initialize x at 98, increment by 7 and stop when x is greater then 266. Something like,
for (int x = 98; x <= 266; x += 7)
System.out.printf("%d = 7 * %d%n", x, x / 7);