programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Iterations - finding the same value for 2 variables

Arjun Bajaj
Greenhorn
Posts: 13
Hi everyone.....

I'm trying to make a program which can calculate the turning effects of rod.....

but i want to calculate the value which the program will get same for both moment and moment2

i tried this but i didn't work.... :

this tells me that i need a boolean rather than an int but it doesn't even allow casting...

so what should i do to get that value which is same for both the variables???

Joanne Neal
Rancher
Posts: 3742
16
= is an assignment operator
== is a comparison operator

David Newton
Author
Rancher
Posts: 12617
In other words: = != ==.

Arjun Bajaj
Greenhorn
Posts: 13
hey,

i tried that but then also it didn't work......

see my current code is :

I dont get any value in the command line.......

What should i do???

David Newton
Author
Rancher
Posts: 12617
Fix your code's logic--it's almost certainly not doing what you think it is. Printing out the iteration count and the two variables you're modifying might be a good place to start.

Rob Spoor
Sheriff
Posts: 21135
87
Let's iterate a bit manually, shall we?

force == 2, distance == 18 => moment == 36
force2 == 2, distance2 == 9 => moment2 == 18

force == 3, distance == 18 => moment == 54
force2 == 3, distance == 9 => moment == 27

force == 4, distance == 18 => moment == 72
force2 == 4, distance == 9 => moment == 36

As you see, the way your code works now moment will always be twice as large as moment2.

So you want to find a moment and moment2 that are equal? Then you should increment either force or force2:
I know find that moment == 36 will break the loop.

If you don't want that break in there you can increase both force and force2 instead. You then get all multiples of 18 except for 0 and 18 itself (since moment will always be larger than 18).

David Newton
Author
Rancher
Posts: 12617
... Seems like giving a hint would have been enough.

Arjun Bajaj
Greenhorn
Posts: 13
hey thanks,
joanne neal, david newton and rob prime for answering.......

hey rob thanks......

i finally solved the problem using your suggestion.......

i just removed the break; method.....and in place of that i put force++

and i added a limit to the while condition and then i got like 100,000 values where those objects can be balanced......

thnx a lot everyone.....

 It is sorta covered in the JavaRanch Style Guide.