1
이 프로그램에서, 나는 옳고 그른 것을 얼마나 많은 질문으로 되 돌리겠습니까. 그러나 내가 쓴 것이 무엇이든 상관없이 나는 20 개의 질문이 정확하고 0은 잘못되었다고 말할 것이다. 누구든지이 문제를 해결하는 방법을 알고 있으므로 그보다 정확합니다.왜 내가 잘못한 게 아니라고 말하는거야?
등급 :
public class KNW_DriverExam
{
//Create the arrays/Declare variable
//Intialize theAnswers array
private String[] theAnswers = {"B" , "D" , "A" , "A" , "C" ,
"A" , "B" , "A" , "C" , "D" ,
"B" , "C" , "D" , "A" , "D" ,
"C" , "C" , "B" , "D" , "A" };
private String[] userAnswers;
int[] missed = new int [theAnswers.length];
/**The DriverExam method, recieves answers
* @param Answer, the answer
* */
public KNW_DriverExam(String[] Answer)
{
userAnswers = new String[theAnswers.length];
for(int i = 0; i < theAnswers.length; i++)
{
userAnswers[i] = theAnswers[i];
}
}
/**The passed method, see if user passes or fails
* @return true if user passed
* @return false if user failed
* */
public boolean passed()
{
if(totalCorrect()>=15)
{
return true;
}
else
{
return false;
}
}
/**The totalCorrect method, see how many user got right
* @return correctCount, how many the user got right
* */
public int totalCorrect()
{
int correctCount = 0;
for(int i = 0; i < theAnswers.length; i++)
{
if(userAnswers[i].equalsIgnoreCase(theAnswers[i]))
{
correctCount++;
}
}
return correctCount;
}
/**The totalIncorrect method, how many the user got wrong
* @return incorrectCount, how many the user got wrong
* */
public int totalIncorrect()
{
int incorrectCount = 0;
for(int i = 0; i < theAnswers.length; i++)
{
if(!(userAnswers[i].equalsIgnoreCase(theAnswers[i])))
{
missed[incorrectCount] = i;
incorrectCount++;
}
}
return incorrectCount;
}
/**The missedQuestions method, how many quetions user missed.
* @return missed, missed questions
* */
public int[] questionsMissed()
{
return missed;
}
}
데모 :
import java.util.Scanner;
public class KNW_DriverExamDemo
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Driver's Exam/n");
System.out.println("20 Multiple Choice Questions Mark A,B,C,D");
//Inputting string
String[] answers = new String[20];
String answer;
for(int i = 0; i < 20; i++)
{
do
{
System.out.println((i + 1) + ": ");
answer = input.nextLine();
}
while(!isValidAnswer(answer));
{
answers[i] = answer;
}
}
KNW_DriverExam exam = new KNW_DriverExam(answers);
System.out.println("Results\n\n");
System.out.println("Total Correct: " + exam.totalCorrect() + "\n");
System.out.println("Total Incorrect: " + exam.totalIncorrect() + "\n");
if(exam.totalIncorrect() > 0)
{
System.out.println("The Incorrect Answers Are: ");
int missedIndex;
for(int i = 0; i < exam.totalIncorrect(); i++)
{
missedIndex = exam.questionsMissed()[i] + 1;
System.out.println(" " + missedIndex);
}
}
}
public static boolean isValidAnswer(String answer)
{
return "A".equalsIgnoreCase(answer) ||
"B".equalsIgnoreCase(answer) ||
"C".equalsIgnoreCase(answer) ||
"D".equalsIgnoreCase(answer);
}
}
Ah : * 실제 * 답변 대신 * 올바른 * 대답을'userAnswers'에 복사합니다. 그게 0 잘못 설명하는 것 같습니다. –