I want to loop from every fourth element in the answer array, and my code is not working.
This is the output I need:
Question 1
Answer 1
Answer 2
Answer 3
Answer 4
Question2
Answer A
Answer B
Answer C
Answer D
This is the output I am getting:
Question 1
Answer 1
Answer 2
Answer 3
Answer 4
Question 2
Answer 1
Answer 2
Answer 3
Answer 4
This is the code I wrote:
String[]question = new String[2];
question[0] = "Question 1";
question[2] = "Question 2";
String[]answer = new String[8];
answer[0] = "Answer 1";
answer[1] = "Answer 2";
answer[2] = "Answer 3";
answer[3] = "Answer 4";
answer[4] = "Answer A";
answer[5] = "Answer B";
answer[6] = "Answer C";
answer[7] = "Answer D";
for (int i = 0; i < question.length; i++) {
System.out.println(question[i]);
for (int j = 0; j<4; j++) {
System.out.println(answer[j]);
}
}
System.out.println(answer[i*4 + j]). Also it should bequestion[1] = "Question 2";(array index can't be equal to its size). Overall it is comfortable to define arrays asString questions[] = {"Question1", "Question2"};