• Create Account

### #Actualifthen

Posted 12 October 2012 - 11:30 AM

alvaro is right. Allow me to demonstrate it another way. I'll read your program as the computer reads it.

public static void main (String[] args){
double start = 0;
double end = 1;
double random = new Random().nextDouble(); //hey Random, assign some number between <0-1) to variable random
double result = start + (random * (end - start)); //now assign 0 + (random * (1-0)) = 0 + (random * 1) = random to variable result
System.out.println(result); //I will tell the user the value of vairable result
//is result is smaller than 0.5
if (result < 0.5)
{ //block T
//I will tell the user that it's a head
//there aren't any statements, so I'll just skip over the block B, there's nothing relevant there
} //end of block T
else
{ //block F
//I will tell the user it's a tail
System.out.println("tail");
//the programmer wants me to do something (compute another flip) more before I end...
//wait... random, end and start probably didn't change, shouldn't the result be the same?
//not my problem, i'll compute it
result = start + (random * (end - start));
//the value of result is the same as before... whatever
//so let's compare again!
if (result < 0.5)
{
//I never get to this, the result is always smaller than 0.5
//but if I did, I would see that there's nothing more and continue after the else branch
}else{  //block F-F
//yeah... we print it
System.out.println("tail");
//ANOTHER TIME? This guy is crazy!
result = start + (random * (end - start));
//and we compare! yes!
if (result < 0.5){
//also never happens
}else{ //block F-F-F
/*and comparing and comparing...
and comparing*/
} // end of block F-F-F
} //end of block F-F <-- this is where I WOULD continue from if the heads come the second time
} //end of block F <-- this is where I continue from if the heads come the first time
//this is where I stop! No more work for me! YEAH!
}

Can you see what is wrong with the code?
1. If the heads come first time, the compiler prints "heads" and skips the whole else branch all down to end of the block F. Nothing else happens.
Solution: Remember, the parent block MUST be executed for the child block to be executed too. So just make all the ifs children of main() block! That way, they will be executed independently. You want to say to computer: "Flip the coin ten times and tell me the result after every flip." Instead, you are saying this: "Flip the coin and tell me the result. If it's tails, do the same thing once more. But flip the coin ten times at most." You must change the code to match its meaning.

2. The computer flips the coin only the first time. Remember, the act of flipping is
random = new Random().nextDouble();

This code
result = start + (random * (end - start));

just converts the result of flipping to another range (or the same range, in your case).

The computer just looks at the coin and says which side is up. But it doesn't flip the coin before checking it, so the result must be the same as before. (Variables, as is the case with coins, usually don't change the side lying up unless moved. ).
SOLUTION: This one is easy, flip the coin (random = new Random().nextDouble();) before checking.

Also, it would be better to do it with procedures, but you seem you need to grasp the meaning of blocks ({}) and ifs. And those are probably the most important flow control in your code, so learn them and love them!

EDIT: How do the code tags work? They don't accept line-breaks?
EDIT 2: Okay, got it. You can't start a code tag with a multi-line comment, or it will be displayed only on one line. Strange.

### #5ifthen

Posted 12 October 2012 - 11:29 AM

alvaro is right. Allow me to demonstrate it another way. I'll read your program as the computer reads it.

public static void main (String[] args){
double start = 0;
double end = 1;
double random = new Random().nextDouble(); //hey Random, assign some number between <0-1) to variable random
double result = start + (random * (end - start)); //now assign 0 + (random * (1-0)) = 0 + (random * 1) = random to variable result
System.out.println(result); //I will tell the user the value of vairable result
//is result is smaller than 0.5
if (result < 0.5)
{ //block T
//I will tell the user that it's a head
//there aren't any statements, so I'll just skip over the block B, there's nothing relevant there
} //end of block T
else
{ //block F
//I will tell the user it's a tail
System.out.println("tail");
//the programmer wants me to do something (compute another flip) more before I end...
//wait... random, end and start probably didn't change, shouldn't the result be the same?
//not my problem, i'll compute it
result = start + (random * (end - start));
//the value of result is the same as before... whatever
//so let's compare again!
if (result < 0.5)
{
//I never get to this, the result is always smaller than 0.5
//but if I did, I would see that there's nothing more and continue after the else branch
}else{  //block F-F
//yeah... we print it
System.out.println("tail");
//ANOTHER TIME? This guy is crazy!
result = start + (random * (end - start));
//and we compare! yes!
if (result < 0.5){
//also never happens
}else{ //block F-F-F
/*and comparing and comparing...
and comparing*/
} // end of block F-F-F
} //end of block F-F <-- this is where I WOULD continue from if the heads come the second time
} //end of block F <-- this is where I continue from if the heads come the first time
//this is where I stop! No more work for me! YEAH!
}

Can you see what is wrong with the code?
1. If the heads come first time, the compiler prints "heads" and skips the whole else branch all down to end of the block F. Nothing else happens.
Solution: Remember, the parent block MUST be executed for the child block to be executed too. So just make all the ifs children of main() block! That way, they will be executed independently. You want to say to computer: "Flip the coin ten times and tell me the result after every flip." Instead, you are saying this: "Flip the coin and tell me the result. If it's tails, do the same thing once more. But flip the coin ten times at most." You must change the code to match its meaning.

2. The computer flips the coin only the first time. Remember, the act of flipping is
random = new Random().nextDouble();

This code
result = start + (random * (end - start));

just converts the result of flipping to another range (or the same range, in your case).

The computer just looks at the coin and says which side is up. But it doesn't flip the coin before checking it, so the result must be the same as before. (Variables, as is the case with coins, usually don't change the side lying up unless moved. ).
SOLUTION: This one is easy, flip the coin (random = new Random().nextDouble();) before checking.

Also, it would be better to do it with procedures, but you seem you need to grasp the meaning of blocks ({}) and ifs. And those are probably the most important flow control in your code, so learn them and love them!

EDIT: How the hell do the code tags work? They don't accept line-breaks?

### #4ifthen

Posted 12 October 2012 - 09:37 AM

alvaro is right. Allow me to demonstrate it another way. I'll read your program as the computer reads it.

/* some declarations */
public static void main (String[] args){
double start = 0;
double end = 1;
double random = new Random().nextDouble(); //hey Random, assign some number between <0-1) to variable random
double result = start + (random * (end - start)); //now assign 0 + (random * (1-0)) = 0 + (random * 1) = random to variable result
System.out.println(result); //I will tell the user the value of vairable result
//is result is smaller than 0.5
if (result < 0.5)
{ //block T
//I will tell the user that it's a head
//there aren't any statements, so I'll just skip over the block B, there's nothing relevant there
} //end of block T
else
{ //block F
//I will tell the user it's a tail
System.out.println("tail");
//the programmer wants me to do something (compute another flip) more before I end...
//wait... random, end and start probably didn't change, shouldn't the result be the same?
//not my problem, i'll compute it
result = start + (random * (end - start));
//the value of result is the same as before... whatever
//so let's compare again!
if (result < 0.5)
{
//I never get to this, the result is always smaller than 0.5
//but if I did, I would see that there's nothing more and continue after the else branch
}else{  //block F-F
//yeah... we print it
System.out.println("tail");
//ANOTHER TIME? This guy is crazy!
result = start + (random * (end - start));
//and we compare! yes!
if (result < 0.5){
//also never happens
}else{ //block F-F-F
/*and comparing and comparing...
and comparing*/
} // end of block F-F-F
} //end of block F-F <-- this is where I WOULD continue from if the heads come the second time
} //end of block F <-- this is where I continue from if the heads come the first time
//this is where I stop! No more work for me! YEAH!
}

Can you see what is wrong with the code?
1. If the heads come first time, the compiler prints "heads" and skips the whole else branch all down to end of the block F. Nothing else happens.
Solution: Remember, the parent block MUST be executed for the child block to be executed too. So just make all the ifs children of main() block! That way, they will be executed independently. You want to say to computer: "Flip the coin ten times and tell me the result after every flip." Instead, you are saying this: "Flip the coin and tell me the result. If it's tails, do the same thing once more. But flip the coin ten times at most." You must change the code to match its meaning.

2. The computer flips the coin only the first time. Remember, the act of flipping is
random = new Random().nextDouble();

This code
result = start + (random * (end - start));

just converts the result of flipping to another range (or the same range, in your case).

The computer just looks at the coin and says which side is up. But it doesn't flip the coin before checking it, so the result must be the same as before. (Variables, as is the case with coins, usually don't change the side lying up unless moved. ).
SOLUTION: This one is easy, flip the coin (random = new Random().nextDouble();) before checking.

Also, it would be better to do it with procedures, but you seem you need to grasp the meaning of blocks ({}) and ifs. And those are probably the most important flow control in your code, so learn them and love them!

EDIT: How the hell do the code tags work? They don't accept line-breaks?

### #3ifthen

Posted 12 October 2012 - 09:36 AM

alvaro is right. Allow me to demonstrate it another way. I'll read your program as the computer reads it.

/* some declarations */
public static void main (String[] args){
double start = 0;
double end = 1;
double random = new Random().nextDouble(); //hey Random, assign some number between <0-1) to variable random
double result = start + (random * (end - start)); //now assign 0 + (random * (1-0)) = 0 + (random * 1) = random to variable result
System.out.println(result); //I will tell the user the value of vairable result
//is result is smaller than 0.5
if (result < 0.5)
{ //block T
//I will tell the user that it's a head
//there aren't any statements, so I'll just skip over the block B, there's nothing relevant there
} //end of block T
else
{ //block F
//I will tell the user it's a tail
System.out.println("tail");
//the programmer wants me to do something (compute another flip) more before I end...
//wait... random, end and start probably didn't change, shouldn't the result be the same?
//not my problem, i'll compute it
result = start + (random * (end - start));
//the value of result is the same as before... whatever
//so let's compare again!
if (result < 0.5)
{
//I never get to this, the result is always smaller than 0.5
//but if I did, I would see that there's nothing more and continue after the else branch
}else{  //block F-F
//yeah... we print it
System.out.println("tail");
//ANOTHER TIME? This guy is crazy!
result = start + (random * (end - start));
//and we compare! yes!
if (result < 0.5){
//also never happens
}else{ //block F-F-F
/*and comparing and comparing...
and comparing*/
} // end of block F-F-F
} //end of block F-F <-- this is where I WOULD continue from if the heads come the second time
} //end of block F <-- this is where I continue from if the heads come the first time
//this is where I stop! No more work for me! YEAH!
}

Can you see what is wrong with the code?
1. If the heads come first time, the compiler prints "heads" and skips the whole else branch all down to end of the block F. Nothing else happens.
Solution: Remember, the parent block MUST be executed for the child block to be executed too. So just make all the ifs children of main() block! That way, they will be executed independently. You want to say to computer: "Flip the coin ten times and tell me the result after every flip." Instead, you are saying this: "Flip the coin and tell me the result. If it's tails, do the same thing once more. But flip the coin ten times at most." You must change the code to match its meaning.

2. The computer flips the coin only the first time. Remember, the act of flipping is
random = new Random().nextDouble();

This code
result = start + (random * (end - start));

just converts the result of flipping to another range (or the same range, in your case).

The computer just looks at the coin and says which side is up. But it doesn't flip the coin before checking it, so the result must be the same as before. (Variables, as is the case with coins, usually don't change the side lying up unless moved. ).
SOLUTION: This one is easy, flip the coin (random = new Random().nextDouble();) before checking.

Also, it would be better to do it with procedures, but you seem you need to grasp the meaning of blocks ({}) and ifs. And those are probably the most important flow control in your code, so learn them and love them!

EDIT: How the hell do the code tags work? They don't accept line-breaks?

### #2ifthen

Posted 12 October 2012 - 09:34 AM

alvaro is right. Allow me to demonstrate it another way. I'll read your program as the computer reads it.

/* some declarations */
public static void main (String[] args){
double start = 0;
double end = 1;
double random = new Random().nextDouble(); //hey Random, assign some number between <0-1) to variable random
double result = start + (random * (end - start)); //now assign 0 + (random * (1-0)) = 0 + (random * 1) = random to variable result
System.out.println(result); //I will tell the user the value of vairable result
//is result is smaller than 0.5
if (result < 0.5)
{ //block T
//I will tell the user that it's a head
//there aren't any statements, so I'll just skip over the block B, there's nothing relevant there
} //end of block T
else
{ //block F
//I will tell the user it's a tail
System.out.println("tail");
//the programmer wants me to do something (compute another flip) more before I end...
//wait... random, end and start probably didn't change, shouldn't the result be the same?
//not my problem, i'll compute it
result = start + (random * (end - start));
//the value of result is the same as before... whatever
//so let's compare again!
if (result < 0.5)
{
//I never get to this, the result is always smaller than 0.5
//but if I did, I would see that there's nothing more and continue after the else branch
}else{  //block F-F
//yeah... we print it
System.out.println("tail");
//ANOTHER TIME? This guy is crazy!
result = start + (random * (end - start));
//and we compare! yes!
if (result < 0.5){
//also never happens
}else{ //block F-F-F
/*and comparing and comparing...
and comparing*/
} // end of block F-F-F
} //end of block F-F <-- this is where I WOULD continue from if the heads come the second time
} //end of block F <-- this is where I continue from if the heads come the first time
//this is where I stop! No more work for me! YEAH!
}


Can you see what is wrong with the code?
1. If the heads come first time, the compiler prints "heads" and skips the whole else branch all down to end of the block F. Nothing else happens.
Solution: Remember, the parent block MUST be executed for the child block to be executed too. So just make all the ifs children of main() block! That way, they will be executed independently. You want to say to computer: "Flip the coin ten times and tell me the result after every flip." Instead, you are saying this: "Flip the coin and tell me the result. If it's tails, do the same thing once more. But flip the coin ten times at most." You must change the code to match its meaning.

2. The computer flips the coin only the first time. Remember, the act of flipping is
random = new Random().nextDouble();

This code
result = start + (random * (end - start));

just converts the result of flipping to another range (or the same range, in your case).

The computer just looks at the coin and says which side is up. But it doesn't flip the coin before checking it, so the result must be the same as before. (Variables, as is the case with coins, usually don't change the side lying up unless moved. ).
SOLUTION: This one is easy, flip the coin (random = new Random().nextDouble();) before checking.

Also, it would be better to do it with procedures, but you seem you need to grasp the meaning of blocks ({}) and ifs. And those are probably the most important flow control in your code, so learn them and love them!

### #1ifthen

Posted 12 October 2012 - 08:34 AM

alvaro is right. Allow me to demonstrate it another way. I'll read your program as the computer reads it.


/* some declarations */
public static void main (String[] args){
double start = 0;
double end = 1;
double random = new Random().nextDouble(); //hey Random, assign some number between <0-1) to variable random
double result = start + (random * (end - start)); //now assign 0 + (random * (1-0)) = 0 + (random * 1) = random to variable result
System.out.println(result); //I will tell the user the value of vairable result
//is result is smaller than 0.5
if (result < 0.5)
{ //block T
//I will tell the user that it's a head
//there aren't any statements, so I'll just skip over the block B, there's nothing relevant there
} //end of block T
else
{ //block F
//I will tell the user it's a tail
System.out.println("tail");
//the programmer wants me to do something (compute another flip) more before I end...
//wait... random, end and start probably didn't change, shouldn't the result be the same?
//not my problem, i'll compute it
result = start + (random * (end - start));
//the value of result is the same as before... whatever
//so let's compare again!
if (result < 0.5)
{
//I never get to this, the result is always smaller than 0.5
//but if I did, I would see that there's nothing more and continue after the else branch
}else{  //block F-F
//yeah... we print it
System.out.println("tail");
//ANOTHER TIME? This guy is crazy!
result = start + (random * (end - start));
//and we compare! yes!
if (result < 0.5){
//also never happens
}else{ //block F-F-F
/*and comparing and comparing...
and comparing*/
} // end of block F-F-F
} //end of block F-F <-- this is where I WOULD continue from if the heads come the second time
} //end of block F <-- this is where I continue from if the heads come the first time
//this is where I stop! No more work for me! YEAH!
}

Can you see what is wrong with the code?
1. If the heads come first time, the compiler prints "heads" and skips the whole else branch all down to end of the block F. Nothing else happens.
Solution: Remember, the parent block MUST be executed for the child block to be executed too. So just make all the ifs children of main() block! That way, they will be executed independently. You want to say to computer: "Flip the coin ten times and tell me the result after every flip." Instead, you are saying this: "Flip the coin and tell me the result. If it's tails, do the same thing once more. But flip the coin ten times at most." You must change the code to match its meaning.

2. The computer flips the coin only the first time. Remember, the act of flipping is
random = new Random().nextDouble();

This code
result = start + (random * (end - start));

just converts the result of flipping to another range (or the same range, in your case).

The computer just looks at the coin and says which side is up. But it doesn't flip the coin before checking it, so the result must be the same as before. (Variables, as is the case with coins, usually don't change the side lying up unless moved. ).
SOLUTION: This one is easy, flip the coin (random = new Random().nextDouble();) before checking.

Also, it would be better to do it with procedures, but you seem you need to grasp the meaning of blocks ({}) and ifs. And those are probably the most important flow control in your code, so learn them and love them!

PARTNERS