比较两个数组,增加计数器
comparing two arrays, increasing counter
我无法理解为什么即使我输入了所有正确的答案,我的计数器仍会打印出不同的值。我已经尝试了我能想到的一切加上研究但仍然没有运气。请帮忙,这是我在这个 "simple" 项目上工作的第 14 个小时。
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 singleAnswer = ' ';
int number_Correct = 0;
for(int i = 0; i < 15; i++) //print question numbers/takes user input
{
System.out.println("Question " + (i + 1) + ":");
singleAnswer = input.nextLine().charAt(0);
}//end of for loop
System.out.println("number correct: " +
total_correct_answers(correctAnswers, singleAnswer));
}//end of main
public static int total_correct_answers(char []correctAnswers,char singleAnswer){
int number_correct = 0;
for (int i = 0; i < 15; i++){
if(correctAnswers[i] == singleAnswer){
number_correct++;}
}//end of for loop
return number_correct;
}//end of correct method
}//end of class
您的程序显示错误值的原因是 singleAnswer
变量仅存储用户最后给出的 value/answer。
我创建了一个数组 userAnswer
来存储所有给出的答案。
试试这个:
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.println("Question " + (i + 1) + ":");
userAnswer [i] = input.nextLine().charAt(0);
}//end of for loop
System.out.println("number correct: " + total_correct_answers(correctAnswers, userAnswer));
input.close();
}//end of main
public static int total_correct_answers(char []correctAnswers,char [] userAnswer) {
int number_correct = 0;
for (int i = 0; i < 15; i++){
if(correctAnswers[i] == userAnswer[i]){
number_correct++;
}
}//end of for loop
return number_correct;
}//end of correct method
}//end of class
对于每个问题,您在数组 correctAnswers 和 user-answer 中都有正确答案?在 singleAnswer 的这种模式下,您只保存用户的最后一个答案并检查每个答案。
要解决这个问题,您可以像这样创建一个 singleAnswer 字符数组:
char[] singleAnswer=new char[15];
并将结果添加到数组中:
singleAnswer[i]=input.nextLine().chatAt(0);
在for循环中用这条指令查看结果是否正确后:
if(singleAnswer[i]==correctAnswers[i]) number_correct++;
我无法理解为什么即使我输入了所有正确的答案,我的计数器仍会打印出不同的值。我已经尝试了我能想到的一切加上研究但仍然没有运气。请帮忙,这是我在这个 "simple" 项目上工作的第 14 个小时。
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 singleAnswer = ' ';
int number_Correct = 0;
for(int i = 0; i < 15; i++) //print question numbers/takes user input
{
System.out.println("Question " + (i + 1) + ":");
singleAnswer = input.nextLine().charAt(0);
}//end of for loop
System.out.println("number correct: " +
total_correct_answers(correctAnswers, singleAnswer));
}//end of main
public static int total_correct_answers(char []correctAnswers,char singleAnswer){
int number_correct = 0;
for (int i = 0; i < 15; i++){
if(correctAnswers[i] == singleAnswer){
number_correct++;}
}//end of for loop
return number_correct;
}//end of correct method
}//end of class
您的程序显示错误值的原因是 singleAnswer
变量仅存储用户最后给出的 value/answer。
我创建了一个数组 userAnswer
来存储所有给出的答案。
试试这个:
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.println("Question " + (i + 1) + ":");
userAnswer [i] = input.nextLine().charAt(0);
}//end of for loop
System.out.println("number correct: " + total_correct_answers(correctAnswers, userAnswer));
input.close();
}//end of main
public static int total_correct_answers(char []correctAnswers,char [] userAnswer) {
int number_correct = 0;
for (int i = 0; i < 15; i++){
if(correctAnswers[i] == userAnswer[i]){
number_correct++;
}
}//end of for loop
return number_correct;
}//end of correct method
}//end of class
对于每个问题,您在数组 correctAnswers 和 user-answer 中都有正确答案?在 singleAnswer 的这种模式下,您只保存用户的最后一个答案并检查每个答案。 要解决这个问题,您可以像这样创建一个 singleAnswer 字符数组:
char[] singleAnswer=new char[15];
并将结果添加到数组中:
singleAnswer[i]=input.nextLine().chatAt(0);
在for循环中用这条指令查看结果是否正确后:
if(singleAnswer[i]==correctAnswers[i]) number_correct++;