0

I'm trying to calculate how many rounds can a player play lotto and joker games with a fixed amount of money.

public static void example () {
    int money = 200;
    int lottoCost = 4;
    int jokerCost = 3;
    int costTogether = lottoCost+jokerCost;
    int rounds = 0;

    for (int i = money; i <= 0; rounds++) {
        money = money-costTogether;
    }

    System.out.println("With " + money + " euros, you can play "
                        + rounds + " rounds.");
    System.out.println("");

}

That code currently prints the text "With 200 euros, you can play 0 rounds." So it doesn't add a +1 to the rounds variable. What am I doing wrong?

6
  • The Loop is never entered that is why you get what you see. Commented Oct 9, 2014 at 10:48
  • 4
    Try doing the for-loop on paper. Commented Oct 9, 2014 at 10:49
  • Why do you use a for loop? Commented Oct 9, 2014 at 10:50
  • If you fix the loop as suggested by others, your output will no longer be correct, since you modify the money variable. Commented Oct 9, 2014 at 10:53
  • Also, you don't need a loop to calculate the number of rounds, normal division will do the trick (and if you use this, then you incidentally "fix" the bug noticed in my previous comment). Commented Oct 9, 2014 at 10:54

5 Answers 5

4

In general, it is good to use the same variable in the 3 parts of the for. Note that the loop initialization part (the first one int i = money) is only run once, and you don't modify i during the loop. Furthermore, the condition is false from the beginning (200 < 0) so the loop is not even run once

I think what you are looking for is a simple int division, just replace your for block with this :

rounds = money / costTogether;
Sign up to request clarification or add additional context in comments.

1 Comment

Upvoted for the simple answer regarding the original problem instead of the OP's current little bug.
2

Your stopping condition is wrong, so the loop is never exectued. You should use >= instead. Also, you never change nor use i.

Here is a corrected version, using currMoney instead of i to be more meaningful.

int rounds = 0;
for (int currMoney = money; currMoney >= costTogether; currMoney -= costTogether) {
    rounds++;
}

But obviously here, you only need a simple division as @Fredszaq pointed out in his answer:

int rounds = money / costTogether;

3 Comments

The money variable should not be changed since it is printed out later on.
Indeed, I thought the OP was printing the remaining money, I hadn't read ;-)
This indeed did the trick! Of course I want a new variable to be used :) Thanks to everyone for really fast answers!
0

This condition i <= 0 is never true and probably dont get incremented

i guess its a typo ,

Greater and Lessthan you need to be aware as they change context .

2 Comments

Infinite loop here though.
@Evan oops ! i didnt see to it thanks . feel free to update it to make it clear
0

Your for-loop is not defined correctly. i starts at money = 200. You want to repeat the loop as long as i <= 0. So you require i starting at 200 while not being larger than 0. That's why your loop isn't executed at all.

Instead prefer a while loop for your case. It is more readable:

while (money >= costTogether) {
    money = money - costTogether;
    rounds++;
}

If you want to use a for loop, you can declare it like that:

for (int i = money; i >= costTogether; i -= costTogether) {
    rounds++;
}

2 Comments

He is doing i <= 0?
This is what his code says. Anyway using a for loop does not seem too appropriated for his case.
-1

You're only incrementing rounds, in a roundabout way, when the loop runs.

The loop runs while i <= 0 - i starts equal to money, which equals 200, so the loop never runs and the rounds value is not incremented.

You're also not changing i in the loop - I suspect you'd rather want while i >= 0 and decrement i in the loop?

for (int i = money; i >= 0; rounds++) {
    money = money-costTogether;
    i--;
}

1 Comment

This is wrong. i starts at money but is not decremented by the right amount at each turn.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.