简体   繁体   中英

How to create an array of integers that equal a certain value?

Hello i am having a tough time trying to write a function that can create an array that holds integers, that equal my simple math problem.

my problem is not adding integers into the array but finding the correct integers that could add up to a math problem.

for example i have a simple math problem like: 10 + 10 = ? we know it equals 20

so i want my array to hold up to ten integers, that when added all together equal 20.

this is what i have been trying in code but not getting the results i want.

    while (totalCount != answer
            && count < setCount) {

        randomNumber = rand.nextInt((int) answer / 2) + 1;

        if(count < setCount) {
            sumOfBalloons.add(randomNumber);
            totalCount += randomNumber;
            count++;
        }

        if(totalCount > answer) {
            count = 0;
            totalCount = 0;
            sumOfBalloons.clear();
        }
    }

i am trying to find random numbers that add up to the math problems answer so i can draw them on balloons. problem is i can never get ten numbers to equal the answer in my while loop.

does anyone know some way of doing something like this?

need array to hold 3 - 10 integers that equals my math problems answer.


** update on code thanks to the advice i received i managed to fix my while loop now it looks like this

had to post like this cause my rep is very low. sorry.

    while (totalCount != answer) {

        randomNumber = rand.nextInt((int) answer / 2) + 1;

        if(totalCount + randomNumber > answer) {
            randomNumber = rand.nextInt((int) answer - totalCount) + 1;
        }

        if(count + 1 == setCount) {
            randomNumber = answer - totalCount;
        }

        if(count < setCount) {
            sumOfBalloons.add(randomNumber);
            totalCount += randomNumber;
            count++;
        }

        if(totalCount > answer
                || totalCount == answer
                && count < setCount
                || totalCount != answer
                && count == setCount) {
            count = 0;
            totalCount = 0;
            sumOfBalloons.clear();
        }
    }

this is what i got in my console from this code

 Total count = 10
 Total totalCount = 20
 sumOfBalloons 0 = 2
 sumOfBalloons 1 = 3
 sumOfBalloons 2 = 3
 sumOfBalloons 3 = 2
 sumOfBalloons 4 = 1
 sumOfBalloons 5 = 4
 sumOfBalloons 6 = 2
 sumOfBalloons 7 = 1
 sumOfBalloons 8 = 1
 sumOfBalloons 9 = 1

I think there are a few options here re: generating random numbers that sum to 20.

Here's one possible solution:

  1. Create an array of length 4, for example.

  2. Generate random number between 1 and 6 for each of the first 3 indices of your array.

    At this point you'll have an array of the form: { 4, 5, 2, _ } (where our 4th element hasn't been chosen yet).

  3. Sum our first 3 elements: 4 + 5 + 2 = 11. Determine 4th element by calculating 20 - current_total (11) = 9.

  4. Set myArray[3] = 9;

A few things to note:

  • You may need to modify the range of possible random numbers ( 1-6 ) I've given. Consider what happens if the array we generate turns out to be { 2, 1, 2, _ }...then there's no digit that will ensure the elements sum to 20.

  • Another option is to use an arrayList instead of an array. The benefit to this is that you can keep adding elements to your arrayList until you either hit 20 (then you're done) or go over (in which case you delete the most recent element and begin adding again). You also won't need (or be able) to know the length of your arrayList in advance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM