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:

Rounding to the nearest twelve

Greenhorn
Posts: 13
This is kinda... unconventional (Never heard that in programming before, have you?).

I'm taking mouse click coordinates in a Jpanel, and dividing each one into regions.
clicking between X coordinates 12 and 23 will count as column 1.
clicking between Y coordinates 0 and 11 will count as row 0.
clicking between X coordinates 60 and 71 will count as column 5.

So, without doing "if X < 11", "if X < 23" ad infinitum (actually ad 27132), how do I round to the nearest twelve?

Ranch Hand
Posts: 255
2
• 2
It looks like division to me.

lowercase baba
Bartender
Posts: 12565
49
Xander Jacob wrote:So, without doing "if X < 11", "if X < 23" ad infinitum (actually ad 27132), how do I round to the nearest twelve?

forget all about programming. in fact, turn off your computer. Get some paper, pencils, and erasers. figure out how YOU would do it with only those devices. If i gave you the number 18,497, what should you round it off to?

Once you know how to do it, write down the steps as if you were explaining them to a PHD in mathematics. Once those steps are crystal clear, revise them so that a grad student could follow them. Revise them again for a high school student. and revise them again until a 10 year old child could follow them.

ONLY when you have those steps figured out - and I literally want you to write them out - should you consider writing a single line of java.

Marshal
Posts: 56610
172
fred rosenberger wrote: . . . write down the steps as if you were explaining them to a PHD in mathematics. Once those steps are crystal clear, revise them so that a grad student could follow them. Revise them again for a high school student. and revise them again until a 10 year old child could follow them. . . .
If your Maths PhD chappie can understand them, then surely so can the ten‑year‑old

Xander Jacob
Greenhorn
Posts: 13
So... a modulo of 12 evaluating to 0? I assumed that modulos only worked with ten. Thanks!

Bartender
Posts: 10575
66
Xander Jacob wrote:So... a modulo of 12 evaluating to 0? I assumed that modulos only worked with ten. Thanks!

Actually, Henry said 'division', not modulo (although what you say is also true). Perhaps this page will help to explain why.

Winston