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++;
}
}
}
}
Sounds simple enough
int b = 0;
while ( b < 20 ) {
answers[b/2] = num[b] + num[b+1];
b = b + 2;
}
A solution quite simply:
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));
}
The explanations are on the comments. :-)
You need to step through the num
array by 2 when adding up answers:
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;
}
}
So, answers
will have 10 items, just like userAnswers
.
You can therefore do, first:
answers = new int[10]
Then you want answers[0] = num[0] + num[1]
. The easiest way would be to go through num
two at a time and add the values to answers
. So:
int ai = 0
for(int i=0; i<20; i+=2)
answers[ai++] = num[i] + num[i+1]
Of course, you don't need to keep a reference to ai
(answer index). You could do instead:
for(int i=0; i<20; i+=2)
answers[i/2] = num[i] + num[i+1]
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.