简体   繁体   English

如何创建等于某个值的整数数组?

[英]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 = ? 例如我有一个简单的数学问题,例如:10 + 10 =? we know it equals 20 我们知道等于20

so i want my array to hold up to ten integers, that when added all together equal 20. 所以我希望我的数组最多容纳十个整数,将它们加在一起等于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. 问题是我永远无法获得十个数字来等于while循环中的答案。

does anyone know some way of doing something like this? 有谁知道做这种事情的某种方式?

need array to hold 3 - 10 integers that equals my math problems answer. 需要数组来保存3-10个等于我数学问题答案的整数。


** update on code thanks to the advice i received i managed to fix my while loop now it looks like this **更新代码,这要归功于我收到的建议,我设法修复了while循环,现在看起来像这样

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. 我认为这里有一些选择:生成总计为20的随机数。

Here's one possible solution: 这是一种可能的解决方案:

  1. Create an array of length 4, for example. 例如,创建一个长度为4的数组。

  2. Generate random number between 1 and 6 for each of the first 3 indices of your array. 为数组的前3个索引中的每个索引生成1到6之间的随机数。

    At this point you'll have an array of the form: { 4, 5, 2, _ } (where our 4th element hasn't been chosen yet). 此时,您将具有以下形式的数组:{4,5,2,_}(此处尚未选择第4个元素)。

  3. Sum our first 3 elements: 4 + 5 + 2 = 11. Determine 4th element by calculating 20 - current_total (11) = 9. 将前三个元素求和:4 + 5 + 2 =11。通过计算20-current_total(11)= 9来确定第四个元素。

  4. Set myArray[3] = 9; 设置myArray[3] = 9;

A few things to note: 注意事项:

  • You may need to modify the range of possible random numbers ( 1-6 ) I've given. 您可能需要修改我提供的可能的随机数范围(1-6)。 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. 考虑一下如果我们生成的数组结果为{2,1,2,_}会发生什么……那么没有数字可以确保元素的总和为20。

  • Another option is to use an arrayList instead of an array. 另一种选择是使用arrayList而不是数组。 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). 这样做的好处是,您可以继续将元素添加到arrayList中,直到达到20(然后完成)或结束(在这种情况下,您删除最新的元素并再次开始添加)。 You also won't need (or be able) to know the length of your arrayList in advance. 您也不需要(或无法)提前知道arrayList的长度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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