生成数组中的随机数,将它们一起添加并将答案存储在数组中

时间:2011-08-18 14:39:07

标签: java arrays random

我是Java新手并试图解决下面的问题

所以这是我的故事: 程序生成最多100个数字,并将每个数字存储在数组中。一旦生成,程序必须添加两个数字并将答案存储在数组中。例如

randomNumber[0]+randomNumber[1]=answer[0]
randomNumber[2]+randomNumber[3]=answer[1]
randomNumber[4]+randomNumber[5]=answer[2]

等。

完成后,我会检查用户输入的答案并计算正确的答案。

问题是:我无法弄清楚如何将两个数字加在一起并将其存储在Array中。这似乎很容易,但我没有足够的经验:)

目前我被困在这里,如果有人能解释或帮助我如何解决这个问题,我将非常感激。

public class gNumber {
    private final int[] num = new int[20]; // array of randomly generated numbers
    private int[] answers; // array of correct answers and UserAnswers
    private final int[] userAnswers = new int[10];
    private int numOfCorrect = 0;

    // Accessor to get the randomly generated number
    int getNumbers(final int n) {
        return num[n];
    }

    int numOfCorrectAnswers() {
        return numOfCorrect;
    }

    // Mutator to store store user input
    void setUsrAnswers(final int _index, final int answer) {
        userAnswers[_index] = answer;
    }

    // Method to generate random 20 numbers
    public void RandomN() {

        for (int i = 0; i < num.length; i++) {
            final int randomNum = (int) (Math.random() * 100);
            num[i] = randomNum;
            final int a = num[i];
            // int b =num[i];
            answers[i] = a + a;
            System.out.println(i + ")" + num[i] + answers[i]);
        }

    }

    // method to add two numbers and store in in answers array
    public void add(int arg1, int arg2) {
        int b = 0;
        while (b < 10) {
            arg1 = num[b];
            arg2 = num[b + 1];
            answers[b] = arg1 + arg2;

            System.out.println(answers[b]);
            b = b + 1;
        }
    }

    // method to check user answers
    public void usrInput(final int usrAnwer) {
        for (final int num : answers) {
            if (num == usrAnwer) {
                numOfCorrect++;
            }
        }

    }
}

4 个答案:

答案 0 :(得分:1)

听起来很简单

int b = 0;
while ( b < 20 ) {
    answers[b/2] = num[b] + num[b+1];
    b = b + 2;
}

答案 1 :(得分:1)

解决方案非常简单:

int N = 20; //20 Random numbers
int[] randomNumber = new int[N];
int[] answer = new int[N/2];

//Generate random numbers of size N
for (int i = 0; i < N; i++) {
    randomNumber = generateRandom(0, 100);
}

//populate answers
for (int i = 0; i < answer.length; i++) {
    answer[i] = randomNumber[i*2] + randomNumber[i*2 + 1];
}

//Generate a random number from [min, max]
private int generateRandom(int min, int max) {
    return min + (int)(Math.random() * ((max - min) + 1));
}

解释见于评论。 : - )

答案 2 :(得分:0)

在添加答案时,您需要逐步浏览num数组:

public void add(int arg1, int arg2) {
    int b = 0;
    while (b < 10) {
        arg1 = num[2*b];
        arg2 = num[2*b + 1];
        answers[b] = arg1 + arg2;

        System.out.println(answers[b]);
        b = b + 1;
    }
}

答案 3 :(得分:0)

因此,answers将有10个项目,就像userAnswers一样。

你可以这样做,首先:

answers = new int[10]

然后你想要answers[0] = num[0] + num[1]。最简单的方法是一次检索两个num并将值添加到answers。所以:

int ai = 0
for(int i=0; i<20; i+=2)
    answers[ai++] = num[i] + num[i+1]

当然,您无需保留对ai(答案索引)的引用。你可以这样做:

for(int i=0; i<20; i+=2)
    answers[i/2] = num[i] + num[i+1]