0

heres the code I need help with.

if (itemAmount >= usedWithAmount) {
    for (int i = 0; i < 10; i++) {
        if (amountToTransfer == 6) {
            usedWithAmount = i;
            break;
        }
        amountToTransfer = itemAmount - i;
        System.out.println(amountToTransfer);
    }
} else if (itemAmount < usedWithAmount) {
    for (int i = 0; i < 10; i++) {
        if (amountToTransfer == 6) {
            itemAmount = i;
            break;
        }
        amountToTransfer = usedWithAmount - i;
    }
}

Alright, people couldn't understand the first time, so I'll go more in depth.

This is used for in my game you have potions. The potions have an amount in them. A potion with (6) at the end, has 6 doses left, (5) has 5 doses left, etcetc.

I'm making it so when you use a potion on another one, it transfers the doses from the potion to the other potion to make it full.

So lets say I have a potion with 5 doses left(represented as itemAmount), and I use it on another potion which has 2 doses left(represented as usedWithAmount).

What I want the algorithm above to do, is use the value of the first potion(itemAmount), and find out how many doses it should transfer to the other potion to make it full(6 doses = full).

So if I used the 5 dose potion with the 2 dose potion, the 5 dose potion should lose 4 doses, while the 2 dose potion gets 4 doses, I want the amountToTransfer(in this case is 4) to represent how many doses to remove from the first item, and to be added to the second item.

4
  • 3
    The best thing to do in this case is to use the debugger to trace your code and see when it does something you weren't expecting. It's one of the more important skills a programmer can learn. What IDE are you using? BlueJ and Eclipse have very easy to use ones Commented Aug 19, 2012 at 21:14
  • So you're saying that usedWithAmount + amountToTransfer = 6? Commented Aug 19, 2012 at 21:22
  • @JonathanBeaudoin I don't think Jagex Ltd. would ask something like this. PS amountToTransfer = Math.min(itemAmount, 6 - usedWithAmount); Commented Aug 19, 2012 at 21:34
  • If only one item could be transferred at one step. The better question would be to count how many ways it could be done. Commented Aug 19, 2012 at 22:14

2 Answers 2

2

See my comment. You can solve this very easily using Math.min.

amountToTransfer = Math.min(itemAmount, 6 - usedWithAmount);

This will return whichever is smaller -- the remainder of the source potion or the amount needed to fill the target potion.


This can also be rewritten:

final int space = 6 - usedWithAmount;
amountToTransfer = itemAmount < space ? itemAmount : space;

or, similar to how David wrote it,

final int space = 6 - usedWithAmount;
if (itemAmount < space) {
  amountToTransfer = itemAmount;
} else {
  amountToTransfer = space;
}
Sign up to request clarification or add additional context in comments.

Comments

1

How about:

// Figure out how much room is available in the one getting the transfer
amountMissing = 6 - usedWithAmount

// If it has room to hold everything, transfer it all
if (amountMissing >= itemAmount)
{
    amountToTransfer = itemAmount;
}
// Otherwise, transfer as much as it can hold
else
{
    amountToTransfer = amountMissing;
}

Comments

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.