Java 问题返回数组
我有一个驾照测试程序,在主方法中,用户输入答案,然后在一个单独的类中,需要有一个方法来查看用户是否通过了一个方法来判断有多少是错的和对的,一个方法将错误的答案数放入一个数组中,然后主方法需要打印结果每一种方法。我的问题是错误的问题数字数组。我相信我正确地创建了它,但我无法获得打印它的主要方法。谢谢Java 问题返回数组,java,arrays,Java,Arrays,我有一个驾照测试程序,在主方法中,用户输入答案,然后在一个单独的类中,需要有一个方法来查看用户是否通过了一个方法来判断有多少是错的和对的,一个方法将错误的答案数放入一个数组中,然后主方法需要打印结果每一种方法。我的问题是错误的问题数字数组。我相信我正确地创建了它,但我无法获得打印它的主要方法。谢谢 public class DriverExam { static String[] correctAnswers={"b","d","a","a","c","a","b","a
public class DriverExam
{
static String[] correctAnswers={"b","d","a","a","c","a","b","a","c","d","b","c","d","a","d","c","c","b","d","a"};
static String[] userAnswers=new String[20];
//constructer
public DriverExam(String[] user)
{
userAnswers=user;
}
//method to see if you passed
public static boolean passed()
{
boolean pass=false;
int correctCount=0;
int incorrectCount=0;
for(int i=0;i<userAnswers.length;i++)
{
if(userAnswers[i].equals(correctAnswers[i]))
{
correctCount++;
}
else
{
incorrectCount++;
}
}
if(correctCount>14)
{pass=true;}
return pass;
}
//method to find number correct
public static int totalCorrect()
{
boolean pass=false;
int correctCount=0;
int incorrectCount=0;
for(int i=0;i<userAnswers.length;i++)
{
if(userAnswers[i].equals(correctAnswers[i]))
{
correctCount++;
}
else
{
incorrectCount++;
}
}
return correctCount;
}
//method to tell how many were not correct
public static int totalIncorrect()
{
boolean pass=false;
int correctCount=0;
int incorrectCount=0;
for(int i=0;i<userAnswers.length;i++)
{
if(userAnswers[i].equals(correctAnswers[i]))
{
correctCount++;
}
else
{
incorrectCount++;
}
}
return incorrectCount;
}
public static int[] questionsMissed()
{
boolean pass=false;
int correctCount=0;
int incorrectCount=0;
for(int i=0;i<userAnswers.length;i++)
{
if(userAnswers[i].equals(correctAnswers[i]))
{
correctCount++;
}
else
{
incorrectCount++;
}
}
int[] questionWrong=new int[incorrectCount];
for(int i=0;i<questionWrong.length;i++)
{
if(!userAnswers[i].equals(correctAnswers[i]))
{i=questionWrong[i];
}
}
return questionWrong;
}
}
import java.util.Scanner;
public class DriverExamDemo
{
public static void main(String[] args)
{
String[] userAnswers=new String[20];
String answer;
System.out.println("please enter the testee's answers as the correspond with the question number");
for(int i=0;i<userAnswers.length;i++)
{
Scanner input=new Scanner(System.in);
System.out.println("Q."+(i+1));
answer=input.next();
userAnswers[i]=answer;
}
// constructor to send the users answers
DriverExam exam1 = new DriverExam(userAnswers);
//Check passed boolean to see if the user passed or failed
if(DriverExam.passed())
System.out.println("you passed your driving exam");
else
System.out.println("you failed your driving exam");
//check how many answers were correct and incorrect
System.out.println("you answered "+DriverExam.totalCorrect()+" questions correctly");
System.out.println("you answered "+DriverExam.totalIncorrect()+" questions incorrectly");
int[] questionsWrong=DriverExam.questionsMissed();
for (int i=0;i<questionsWrong.length;i++)
{
System.out.println("you got question " +questionsWrong[i]+"incorrect");
}
}
}
公共类driverxam
{
静态字符串[]correctAnswers={“b”、“d”、“a”、“a”、“c”、“a”、“b”、“a”、“c”、“d”、“b”、“c”、“d”、“a”、“d”、“c”、“c”、“b”、“d”、“a”};
静态字符串[]userAnswers=新字符串[20];
//建设者
公共驱动器RAM(字符串[]用户)
{
userAnswers=用户;
}
//方法来查看您是否通过了
公共静态布尔传递()
{
布尔传递=假;
int correctCount=0;
int incorrectCount=0;
对于(int i=0;i14)
{pass=true;}
回程通行证;
}
//找到正确数字的方法
公共静态整数totalCorrect()
{
布尔传递=假;
int correctCount=0;
int incorrectCount=0;
对于(int i=0;i检查下一行,因为它没有意义,您正在为循环控制变量分配空数组的值
i = questionWrong[i];
但是,您用来检查错误问题数量的整个算法都是错误的,您应该尝试使用另一个算法。使用您的实现,您只需删除整个问题的一个子集,您应该循环检查所有问题,并获得错误问题的数量
顺便说一句,我认为您正在大量使用静态方法。从广义上讲,最好使用实例方法,并且对于您的案例来说是完全有效的。在您的案例中使用实例方法,允许在构造函数中进行所有计算,或由构造函数进行方法调用,并避免重复相同的代码(循环)所有的代码。你能不能至少提一下编程语言(并相应地标记你的问题)?…然后在这里和那里按
?用java编程,我不知道还有什么可以标记它。我有一个数组的问题,所以我把它标记为数组我显然是新的,这是我的错