我希望我的程序能够显示两个阵列之间错过的问题。我会输入前12个的所有正确答案,然后输入错误的最后3个答案。每当我尝试显示它时,它会产生一些奇怪的返回答案,我认为它可能是内存地址。我已尽力将其打印出错过的问题编号。请任何帮助将非常感谢!
import java.util.Scanner; //import scanner
public class DriverTestBlah {
public static void main(String [] args){
Scanner input = new Scanner(System.in);
char[] correctAnswers = {'A','D','C','A','A','D','B',
'A','C','A','D','C','B','A','B'};
char[] userAnswer = new char[correctAnswers.length];
for(int i = 0; i < 15; i++) //print question numbers/takes user input
{
System.out.print("Question " + (i + 1) + ":");
userAnswer [i] = input.nextLine().charAt(0);
}//end of for loop
for(int i = 0; i < questions.length; i++) //for loop prints you missed
{
System.out.print("You missed question: ");
for(int y = 0; y < 1; y++) //for loop only takes one question # at a time
{
System.out.println(" " + which_questions_missed(userAnswer, correctAnswers));
}//end of question number for loop
}//end of
}//end of main
//create new class that determines which questions missed and returns their numbers
public static int []which_questions_missed(char[] userAnswer, char[] correctAnswers){
int missed[] = new int[correctAnswers.length];
for (int i = 0; i < correctAnswers.length; i++){
if (userAnswer[i] != correctAnswers[i]){
missed[i]=(i+1);//chooses the index of the missed answers and adds 1 to display
}
}
return missed;
}
}//end of class
答案 0 :(得分:0)
我会像这样修改方法(不是类)which_questions_missed
。请注意我是如何将名称从which_questions_missed
更改为whichQuestionsMissed
这在Java程序中不是偶然使用camelCase而不是snake_case:
import java.util.Scanner;
public class Main {
public static void main(String [] args){
Scanner input = new Scanner(System.in);
char[] correctAnswers = {'A','D','C','A','A','D','B', 'A','C','A','D','C','B','A','B'};
char[] userAnswer = new char[correctAnswers.length];
for(int i = 0; i < 15; i++) {
System.out.print("Question " + (i + 1) + ":");
userAnswer[i] = input.nextLine().charAt(0);
}
whichQuestionsMissed(userAnswer, correctAnswers);
}
public static void whichQuestionsMissed(char[] userAnswer, char[] correctAnswers) {
for(int i = 0; i < userAnswer.length; i++) {
if(Character.toUpperCase(userAnswer[i]) != correctAnswers[i]) {
System.out.println("You missed question: " + i);
}
}
}
}
它利用Character.toUpperCase()
,允许用户输入等效的小写字母。
试试here!