simply looping the array, comparing the values at the same indices, and depending on the values, sum up the scores...
giving +4 for each correct answer, -1 for each incorrect answer, and +0 for each blank answer.
simply looping the array, comparing the values at the same indices, and depending on the values, sum up the scores...i think i got it now
public int scoreUp(String[] key, String[] answers) {
int count=0;
for(int i=0;i<key.length;i++){
if(( (!key[i].equals("?")) &&(!answers.equals("?")) &&(key[i]==answers[i]))){
count=count+4;
}else{
count=count-1;
}
}
return count;
}
Expected Run
scoreUp(["a", "a", "b", "b"], ["a", "c", "b", "c"]) â†’ 6 6 OK
scoreUp(["a", "a", "b", "b"], ["a", "a", "b", "c"]) â†’ 11 11 OK
scoreUp(["a", "a", "b", "b"], ["a", "a", "b", "b"]) â†’ 16 16 OK
scoreUp(["a", "a", "b", "b"], ["?", "c", "b", "?"]) â†’ 3 1 X
scoreUp(["a", "a", "b", "b"], ["?", "c", "?", "?"]) â†’ -1 -4 X
scoreUp(["a", "a", "b", "b"], ["c", "?", "b", "b"]) â†’ 7 6 X
scoreUp(["a", "a", "b", "b"], ["c", "?", "b", "?"]) â†’ 3 1 X
scoreUp(["a", "b", "c"], ["a", "c", "b"]) â†’ 2 2 OK
scoreUp(["a", "a", "b", "b", "c", "c"], ["a", "c", "a", "c", "a", "c"]) â†’ 4 4 OK
scoreUp(["a", "a", "b", "b", "c", "c"], ["a", "c", "?", "?", "a", "c"]) â†’ 6 4 X
scoreUp(["a", "a", "b", "b", "c", "c"], ["a", "c", "?", "?", "c", "c"]) â†’ 11 9 X
scoreUp(["a", "b", "c"], ["a", "b", "c"]) â†’ 12 12 OK
other tests
X
"?" representing a question left blank.this will appear in the "answers" array. Therefore the following
(!key[i].equals("?"))
is not necessary. Your errors occur when there is a "?" in the answers. When it is found you add -1. That is wrong.
The challenge states
"?" representing a question left blank.
this will appear in the "answers" array. Therefore the following
(!key[i].equals("?"))
is not necessary. Your errors occur when there is a "?" in the answers. When it is found you add -1. That is wrong.
public int scoreUp(String[] key, String[] answers) {
int count=0;
for(int i=0;i<key.length;i++){
if(( (!key[i].equals("?")) &&(!answers.equals("?")) &&(key[i]==answers[i]))){
count=count+4;
}else if(key[i].equals("?")){
count=0;
}else if(answers[i].equals("?")){
count=0;
}else{
count=count-1;
}
}
return count;
}
Expected Run
scoreUp(["a", "a", "b", "b"], ["a", "c", "b", "c"]) â†’ 6 6 OK
scoreUp(["a", "a", "b", "b"], ["a", "a", "b", "c"]) â†’ 11 11 OK
scoreUp(["a", "a", "b", "b"], ["a", "a", "b", "b"]) â†’ 16 16 OK
scoreUp(["a", "a", "b", "b"], ["?", "c", "b", "?"]) â†’ 3 0 X
scoreUp(["a", "a", "b", "b"], ["?", "c", "?", "?"]) â†’ -1 0 X
scoreUp(["a", "a", "b", "b"], ["c", "?", "b", "b"]) â†’ 7 8 X
scoreUp(["a", "a", "b", "b"], ["c", "?", "b", "?"]) â†’ 3 0 X
scoreUp(["a", "b", "c"], ["a", "c", "b"]) â†’ 2 2 OK
scoreUp(["a", "a", "b", "b", "c", "c"], ["a", "c", "a", "c", "a", "c"]) â†’ 4 4 OK
scoreUp(["a", "a", "b", "b", "c", "c"], ["a", "c", "?", "?", "a", "c"]) â†’ 6 3 X
scoreUp(["a", "a", "b", "b", "c", "c"], ["a", "c", "?", "?", "c", "c"]) â†’ 11 8 X
scoreUp(["a", "b", "c"], ["a", "b", "c"]) â†’ 12 12 OK
other tests
X
public int scoreUp(String[] key, String[] answers) {
int count=0;
for(int i=0;i<key.length;i++){
if(( (!key[i].equals("?")) &&(!answers.equals("?")) &&(key[i]==answers[i]))){
count=count+4;
}else if(key[i].equals("?")){
count=0;
}else if(answers[i].equals("?")){
count=count+0;
}else if(!answers[i].equals(key[i])){
count=count-1;
}
}
return count;
}
something like above."key" array is an array containing the correct answers to an examTherefore a "?" will never be found in the key array. So,
(!key[i].equals("?"))
and
else if(key[i].equals("?")){
count=0;
are not necessary.count=count+0;
That is confusing for the reader. You could just use
continue;
public int scoreUp(String[] key, String[] answers) {
int count=0;
for(int i=0;i<key.length;i++){
if(( (!answers.equals("?")) &&(key[i]==answers[i]))){
count=count+4;
}/*else if(key[i].equals("?")){
continue;
}*/else if(answers[i].equals("?")){
count=count+0;
}else if(!answers[i].equals(key[i])){
count=count-1;
}
}
return count;
}
above looks ok?It looks ok, but you could clean it up to improve readability.
public int scoreUp(String[] key, String[] answers) {
int count = 0;
for(int i=0;i<key.length;i++){
if(answers[i].equals("?"))continue;
if(key[i]==answers[i]){
count = count + 4;
}else count--;
}
return count;
}
If you are experiencing a similar issue, please ask a related question
Title | # Comments | Views | Activity |
---|---|---|---|
thymeleaf natural templating vs JSP | 2 | 66 | |
how do i compare an object based on two fields | 6 | 48 | |
How to increase Spring boot/Tomcat max file upload size | 2 | 19 | |
Problem to error | 4 | 43 |
Join the community of 500,000 technology professionals and ask your questions.
Connect with top rated Experts
22 Experts available now in Live!