I have been trying to generate a Java code for this, but I need help. I have a survey with 10
questions and each question can be answered with a number between 1
and 5
. I need to create a list of all possible combinations. All questions are mandatory.
With this, I have the following:
Questions: 10
Answers: 1,2,3,4,5
The desired output is like this:
1111111111
1111111112
1111111113
1111111114
1111111115
1111111121
1111111122
1111111123
1111111124
1111111125
1111111131
1111111132
1111111133
1111111134
1111111135
1111111141
1111111142
. .
5555555555
Optionally the code can have the option to change number of questions: instead of fixed to 10
, change it to 6
or 12
, and also the number of answers: instead of only 1
to 5
, change it to 1
to 4
.
This is the code I wrote:
try {
PrintWriter pw = new PrintWriter(new FileWriter(
"C:\Users\User\Desktop\Test\prueba.txt"));
int numResultados = 20;
int numPreguntas = 10;
int respuestaMax = 5;
int respuestaMin = 1;
int[] input = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
int posicion = 0;
for (int k = 0; k <= numResultados; k++) {
for (int j = 0; j < respuestaMax; j++) {
for (int i = 0; i < input.length; i++) {
System.out.print(input[i]);
}
System.out.println();
input[posicion]++;
}
input[posicion] = 1;
posicion++;
}
} catch (Exception e) {
e.printStackTrace();
}
but the output I receive is not the one I'm expecting and I'm stucked, extract:
1111111111
2111111111
3111111111
4111111111
5111111111
1111111111
1211111111
1311111111
1411111111
1511111111
1111111111
1121111111
1131111111
1141111111
1151111111
1111111111
1112111111
1113111111
1114111111
1115111111