d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Easiest Java Question
Add Reply New Topic New Poll
Member
Posts: 6,604
Joined: May 29 2013
Gold: 2.72
Sep 10 2014 07:47am
So, I just started doing some Java in my class and we work with eclipse.

So ..... heres my problem.

So I have to do something .....

Let's say Marc has 100 balls ..... and he wins something and his friends has to give him some of his balls
Marie has to give him all her balls, John has to give him half of his and Simon has to give him 3/4 of his.


So what I did was just to *0,5 for john and *0,75 for simon .... but at the end, Marc cannot end up with 300,5 balls, it has to be a round number

And I aint sure how to do that, I think its with modulo but I have no clue how to use it.



Alright thx, would be nice if someone could help me with that!
Retired Moderator
Posts: 21,073
Joined: Apr 7 2008
Gold: 5,135.90
Trader: Trusted
Sep 10 2014 08:52am
http://www.dreamincode.net/forums/topic/273783-the-use-of-the-modulo-operator/

That is information on modulo.


http://www.tutorialspoint.com/java/number_round.htm
That is java rounding. Your assignment will probably specify if you need to round up or down(ie person has 15 balls but has to give away half, do they give 7 or 8?)

If you have a decimal you can round down all the time with math.floor as well
Member
Posts: 6,604
Joined: May 29 2013
Gold: 2.72
Sep 10 2014 09:06am
Quote (Kagura @ Sep 10 2014 10:52am)
http://www.dreamincode.net/forums/topic/273783-the-use-of-the-modulo-operator/

That is information on modulo.


http://www.tutorialspoint.com/java/number_round.htm
That is java rounding. Your assignment will probably specify if you need to round up or down(ie person has 15 balls but has to give away half, do they give 7 or 8?)

If you have a decimal you can round down all the time with math.floor as well


Well I guess you cant have .5 of a ball, so I guess I have to round down.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 10 2014 04:23pm
Not sure why you're using modulo.

Instead of multiplying both people by a ratio, i suggest a different approach:
1) figure out how many balls are changing hands (use your rounding logic here)
2) take those balls away from giver
3) give those balls to receiver

eg:
Code
class Person{
private int _balls;

void takeAwayBalls(int balls){
_balls -+ balls;
}

void giveBalls(int balls){
_balls += balls;
}

void giveBallsTo(Person receiver, double ratio){
int ballsToGive = magicalroundfunction(ratio * _balls);
this.takeAwayBalls(ballsToGive);
marc.giveBalls(ballsToGive);
}
}


where magicalroundfunction is whatever your rounding logic is.

use some better naming convention
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Sep 11 2014 09:56am
hehe, "balls"
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll