Write a program that stores answers to ten questions in a character array. Each
ID: 3740331 • Letter: W
Question
Write a program that stores answers to ten questions in a character array. Each question has one correct answer and four possible answers. (A,B,C,D).
Prompt the user for ten guesses and store the user’s answers in another array.
After the user is done entering the answers, show them the correct answer, their answer, and whether they got the question correct or not.
Also show them how many they got right, and how many they got wrong.
char guesses[10]; //this will store the user answers
Sample Execution:
Enter your guess for Question 1 (A,B,C,D): A
Enter your guess for Question 2 (A,B,C,D): B
Enter your guess for Question 3 (A,B,C,D): F
ERROR!! Guess must be A,B,C,D
Enter your guess for Question 3 (A,B,C,D): D
…
Enter your guess for Question 10 (A,B,C,D): D
Here are the results of your quiz:
Question 1: Correct Answer: A Your Answer: A Correct.
Question 2: Correct Answer: B Your Answer: B Correct.
Question 3: Correct Answer: C Your Answer: D Incorrect.
…
Question 10: Correct Answer: A Your Answer: D Incorrect.
You got 4 questions right.
You got 6 questions wrong.
You got 40%.
Explanation / Answer
Here is the below code :
public class MyClass {
public static void main(String []args){
char answers[] = {'A', 'B', 'C', 'D', 'B', 'A', 'B', 'C', 'D', 'A'};
char correctAnswer[] = {'B', 'C', 'A', 'D','B', 'A', 'B', 'C', 'D', 'A'};
int count = 0;
for(int i = 0; i<= 10 ; i++){
if (answers[i] == correctAnswer[i]){
System.out.println("Correct answer");
count = count + correctAnswer[i];
System.out.println(count);
}
else{
System.out.println(" WRONG ANSWER , Correct answer is : " + correctAnswer[i]);
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.