How do I make this char answer [][] given input in the code change to user input instead ? sample input is when I enter 9 , I will enter 9 student answer. the answer column contains 10 answer in character. The sample output is at the bottom below the code.
input:
9
DBDCCDAEAD
ABACCDEEAD
DBABCAEEAD
EDDACBEEAD
CBAEDCEEAD
ABDCCDEEAD
BBECCDEEAD
BBACCDEEAD
EBECCDEEAD
DBDCCDEEAD
public class Q3 {
public static void main (String[] args) {
char answer[][] =
{
{'A','B','A','C','C','D','E','E','A','D'},
{'D','B','A','B','C','A','E','E','A','D'},
{'E','D','D','A','C','B','E','E','A','D'},
{'C','B','A','E','D','C','E','E','A','D'},
{'A','B','D','C','C','D','E','E','A','D'},
{'B','B','E','C','C','D','E','E','A','D'},
{'B','B','A','C','C','D','E','E','A','D'},
{'E','B','E','C','C','D','E','E','A','D'}
};
char keys[] = {'D','B','D','C','C','D','A','E','A','D'};
double[] student = new double[answer.length];
for(int i = 0; i < answer.length; ++i)
{
double right = 0;
double wrong = 0;
double w =0;
for(int j = 0; j < answer[i].length; ++j)
{
if(answer[i][j] == keys[j]) {
++right; }
if(answer[i][j] != keys[j]) {
++wrong;
w = wrong - (wrong * 0.25);
}
}
student[i] = right + w ; // right - wrong
}
for(int i = 0; i < student.length; ++i)
System.out.println("student"+(i+1)+ " " + student[i]);
}
}
output:
Student 1: 6.25
Student 2: 5.00
Student 3: 3.75
Student 4: 2.50
Student 5: 7.50
Student 6: 6.25
Student 7: 6.25
Student 8: 6.25
Student 9: 8.75
You only have 8 rows in your answer array. Assuming the first row is the key and the rest are answers, fill in the arrays as follows to avoid errors.
String[] s = { "DBDCCDAEAD", "ABACCDEEAD", "DBABCAEEAD",
"EDDACBEEAD", "CBAEDCEEAD", "ABDCCDEEAD",
"BBECCDEEAD", "BBACCDEEAD", "EBECCDEEAD",
"DBDCCDEEAD" };
// compute the key
char[] keys = s[0].toCharArray();
// fill in the answers
char answer[][] = new char[s.length-1][];
for(int i = 1; i < s.length; i++) {
answer[i-1] = s[i].toCharArray();
}
I would also make the following changes.
for (int j = 0; j < answer[i].length; j++) {
if (answer[i][j] == keys[j]) {
++right; // <- if it isn't right, it must wrong.
} else {
++wrong;
w = wrong - (wrong * 0.25); // Move this outside the inner loop
// since you only use the last value of
// wrong
}
}
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.