This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Java in General and the fly likes Iterations - finding the same value for 2 variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Iterations - finding the same value for 2 variables" Watch "Iterations - finding the same value for 2 variables" New topic
Author

Iterations - finding the same value for 2 variables

Arjun Bajaj
Greenhorn

Joined: Apr 21, 2010
Posts: 13
Hi everyone.....

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

i have made this :


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???

please help......its my physics homework.......
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3456
    
  12
= is an assignment operator
== is a comparison operator


Joanne
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

In other words: = != ==.
Arjun Bajaj
Greenhorn

Joined: Apr 21, 2010
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???

please help.......
David Newton
Author
Rancher

Joined: Sep 29, 2008
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

Joined: Oct 27, 2005
Posts: 19672
    
  18

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).


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

... Seems like giving a hint would have been enough.
Arjun Bajaj
Greenhorn

Joined: Apr 21, 2010
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.....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Iterations - finding the same value for 2 variables