I am new to Java and trying to solve the problem below
So this is my story:
Program generates 20 numbers up to 100 and stores every number in array. Once generated the program has to add two numbers and stores the answer in array.. Example
randomNumber[0]+randomNumber[1]=answer[0]
randomNumber[2]+randomNumber[3]=answer[1]
randomNumber[4]+randomNumber[5]=answer[2]
etc..
Once done I check user input with answers and count the correct answers.
The problem is: I can’t figure out how to add two numbers together and store it in Array. It seems easy but I am not experienced enough 🙂
Currently I am stuck here, I would really appreciate if someone could explain or help me how to solve this problem.
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++;
}
}
}
}
A solution quite simply:
The explanations are on the comments. 🙂