(cast) iteration not working???

R

Radith

public class Random {
public static void main(String[] args) {

double i = Math.random() * 100;
int j = 0;
while ((int) i == 90) {
i = Math.random() * 100;
j++;
}

System.out.println("It took " + j " batches to produce the double 90
using a cast");

}
}


THERE SEEMS TO BE THREE ERRORS WITH THE ABOVE CODE; CAN YOU PLS. HELP:
* There's an error in System.out.println
* j doesn't increment
* calculation / while statement seems to be incorrect. IS the cast a
prob. here?

Sorry this maybe simple stuff but I'm still a begginer.

All help Appreciated. Thanks in advance.

Cheers
 
R

Radith

But When I Go:
Math.random() * 100;

Doesn't that mean it creates a number < 100

Cheers
hilz said:
the test condition of the while loop is the problem:
((int) i == 90
i is between 0 and 100
so what are the chances that i==90 ?
i think it should be i!=90


Radith said:
public class Random {
public static void main(String[] args) {

double i = Math.random() * 100;
int j = 0;
while ((int) i == 90) {
i = Math.random() * 100;
j++;
}

System.out.println("It took " + j " batches to produce the double 90
using a cast");

}
}


THERE SEEMS TO BE THREE ERRORS WITH THE ABOVE CODE; CAN YOU PLS. HELP:
* There's an error in System.out.println
* j doesn't increment
* calculation / while statement seems to be incorrect. IS the cast a
prob. here?

Sorry this maybe simple stuff but I'm still a begginer.

All help Appreciated. Thanks in advance.

Cheers
 
H

hilz

the test condition of the while loop is the problem:
((int) i == 90
i is between 0 and 100
so what are the chances that i==90 ?
i think it should be i!=90
 
A

Andy Hill

Radith said:
But When I Go:
Math.random() * 100;

Doesn't that mean it creates a number < 100

Cheers
Yup. But you're using a "while" construct, not an "until" (which java doesn't
have). Since you want to loop "until i == 90", that is equivalent to looping
"while i != 90".
 
H

hilz

Radith said:
But When I Go:
Math.random() * 100;

Doesn't that mean it creates a number < 100

yes, and if this number was not 90, it will just call the system.out.println
statement and exits.
so what you need is for the while loop to execute until the result is 90 and
then exit,
and that would be
while((int)i!=90)

if you find that hard to understand, try to debug the program. Are you using
an IDE that has debug capability? If not, then I suggest you download
Netbeans from netbeans.org and that will help you debug your code.

And as mentioned in another post, your system.out.println statement is
missing a + after the j.

good luck.
hilz

P.S. You might want to stick to posting to comp.lang.java.help at this
stage. That group is more suitable for beginner questions.
 
R

Radith

Radith said:
public class Random {
public static void main(String[] args) {

double i = Math.random() * 100;
int j = 0;
while ((int) i == 90) {
i = Math.random() * 100;
j++;
}

System.out.println("It took " + j " batches to produce the double
90 using a cast");

}
}


THERE SEEMS TO BE THREE ERRORS WITH THE ABOVE CODE; CAN YOU PLS. HELP:
* There's an error in System.out.println
* j doesn't increment
* calculation / while statement seems to be incorrect. IS the cast a
prob. here?

Sorry this maybe simple stuff but I'm still a begginer.

All help Appreciated. Thanks in advance.

Cheers
 
S

Sudsy

Andy Hill wrote:
Yup. But you're using a "while" construct, not an "until" (which java doesn't
have). <snip>

But there's always the following construct:

do {
// something
} while ( // condition );

Same as in C.
 
H

hilz

But there's always the following construct:

do {
// something
} while ( // condition );

Same as in C.

but that is still not the same as "until"
"until" keeps the control in the loop until the condition is met.
"while" keeps the control in the loop while the condition is met
so to get the "until" effect using a "while", you will have to reverse the
"while" condition. A "do..while" construct does not do that.

thanks
hilz
 
S

Sudsy

hilz wrote:
but that is still not the same as "until"
"until" keeps the control in the loop until the condition is met.
"while" keeps the control in the loop while the condition is met
so to get the "until" effect using a "while", you will have to reverse the
"while" condition. A "do..while" construct does not do that.

Then use the shortcut for(;;until_condition){}. There are always
equivalents in most languages, Java included... ;-)
 
E

Eric Sosman

Sudsy said:
hilz wrote:



Then use the shortcut for(;;until_condition){}. There are always
equivalents in most languages, Java included... ;-)

That's an infinite loop, suitable for "until Hell
freezes."
 
S

Sudsy

Eric said:
Sudsy wrote:


That's an infinite loop, suitable for "until Hell
freezes."

Sorry, I meant for(;until_condition;) {}. Shouldn't post when I'm
tired... :-(
 
C

Chris Smith

Sudsy said:
Sorry, I meant for(;until_condition;) {}. Shouldn't post when I'm
tired... :-(

I think you're still missing the point. The condition is still inverted
compared to, for example, Pascal's "Repeat ... Until ..." construct. In
fact, "for (;cond;)" is completely equivalent to "while (cond)", except
that it's more confusing.

If you really want to come up with something, you could suggest using
"while (!(cond))" or "do { ... } while (!(cond));", but it's not really
pedagogically sound to explain that as a separate construct from any
other while or do/while loop with a boolean expression.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top