gpt4 book ai didi

java - 为什么说我没弄错?

转载 作者:行者123 更新时间:2023-11-29 07:30:21 26 4
gpt4 key购买 nike

在这个程序中,我应该返回我答对和错了多少题。但无论我记下什么,它都会说我答对了 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);
}

最佳答案

看看你的构造函数。当您分配给 userAnswers 时,您使用的是 theAnswers 而不是提供的 Answer

public KNW_DriverExam(String[] Answer) {
userAnswers = new String[Answers.length];

for(int i = 0; i < Answers.length; i++) {
userAnswers[i] = Answers[i];
}
}

关于java - 为什么说我没弄错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43714100/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com