Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8396149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:28:04+00:00 2026-06-09T20:28:04+00:00

heres the code I need help with. if (itemAmount >= usedWithAmount) { for (int

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T20:28:06+00:00Added an answer on June 9, 2026 at 8:28 pm

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello people I need help please. Here's my code. In here is a list
I need help with a weird runtime error. Here's the code which gives it:
I need help... I got errors with my android xml... Here's the code: <?xml
BeautifulSoup newbe... Need help Here is the code sample... from mechanize import Browser from
i need ur help regarding web services in classic asp here is my code
I need help re-factoring this legacy LINQ-SQL code which is generating around 100 update
Need help with deleting call log from multi numbers, using the following code i
I need some help with types in Haskell... Here's the code I'm working on:
Here's some code I am currently using. Where I need help are $bn_name_search and
Here my code, I need to insert into mysql database I have mysql,php,android 1)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.