+ 1
Can anyone please help explain the flow of this code... (I'd like to know why the total count is 10)
class Echo { int count = 0; void hello () { System.out.println ("helloooo..."); } } public class EchoTestDrive { public static void main (String[] args){ Echo e1 = new Echo (); Echo e2 = new Echo (); int x = 0; while (x<4) { e1.hello(); e1.count = e1.count + 1; if (x==3) { e2.count = e2.count + 1; } if (x>0) { e2.count = e2.count + e1.count; } x = x + 1; } System.out.println (e2.count); } } the output is: helloooo... helloooo... helloooo... helloooo... 10
3 odpowiedzi
+ 1
Thanks Matt Wolf! I now get it. So the value of the x different/independent from the value of that of the e1 and e2. Is that right? I wrote down the flow in paper using your explanation and it made sense now! Thanks!
0
class Echo {
int count = 0;
void hello () {
System.out.println ("helloooo...");
}
}
public class EchoTestDrive {
public static void main (String[] args){
Echo e1 = new Echo ();
Echo e2 = new Echo ();
int x = 0;
while (x<4) {
e1.hello();
e1.count = e1.count + 1; /* e1.count added to here*/
if (x==3) {
e2.count = e2.count + 1; /*only when x is equal to 3 does e2.count get added to here, this occurs when e.2count is equal to 6 making it 7. it happens in the last loop.*/
}
if (x>0) {
e2.count = e2.count + e1.count; /*here with e2.count having a value of 7 e1.count has a value of 3 */
}
x = x + 1;
} /*closes the while loop*/
System.out.println (e2.count); /*e2.count is equal to 10*/
}
}
Yea. I just added a few system outs to your code to show the variables at the end of each loop note the values between hellos (note the order of the variable print out
class Echo {
int count = 0;
void hello () {
System.out.println ("helloooo...");
}
}
public class EchoTestDrive {
public static void main (String[] args){
Echo e1 = new Echo ();
Echo e2 = new Echo ();
int x = 0;
while (x<4) {
e1.hello();
e1.count = e1.count + 1;
if (x==3) {
e2.count = e2.count + 1;
}
if (x>0) {
e2.count = e2.count + e1.count;
}
System.out.println(e1.count);
System.out.println(e2.count);
System.out.println(x);
x = x + 1;
}
System.out.println (e2.count);
}
}
0
public class Echo {
int count=0;
void hello(){
System.out.println("helloooo...");
}
}
public class EchoTestDrive {
public static void main(String[] args) {
Echo e1=new Echo();
Echo e2=new Echo();
int x = 0;
while ( x < 4){
e1.hello();
e1.count = e1.count + 1;
if ( x > 0){
e2.count = e2.count + 1;
}
if ( x > 1) {
e2.count = e2.count + e1.count;
}
x = x + 1;
}
System.out.println(e2.count);
}
}
Output
----------
helloooo...
helloooo...
helloooo...
helloooo...
10
=========================================
public class Echo {
int count=0;
void hello(){
System.out.println("helloooo...");
}
}
public class EchoTestDrive {
public static void main(String[] args) {
Echo e1=new Echo();
Echo e2=e1;
int x = 0;
while ( x < 4){
e1.hello();
e1.count = e1.count + 1;
if ( x > 0){
e2.count = e2.count + 1;
}
if ( x > 1) {
e2.count = e2.count + e1.count;
}
x = x + 1;
}
System.out.println(e2.count);
}
}
Output
---------
helloooo...
helloooo...
helloooo...
helloooo...
24