为什么我的计数器不计数,而我的ArrayList没有将新的整数值添加到数组中



我的问题主要来源于我的方法"add"、数组"mathAnswers"和计数器"score">

我正在尝试比较数组值,使它们执行数学函数,然后检查用户输入是否与答案匹配。我还希望将正确的答案添加到数组中,然后打印出该数组,以及验证用户分数的计数器。

我遇到了一个问题,我的计数器没有递增,我的值没有输入到数组中,所有值都保持为0。这是我的密码。

import java.util.Scanner;
import java.util.ArrayList;
public class Program7 {
public static void main(String[] args) {
int [] mathQuizLeft = {
8,  //0
14, //1
16, //2
8,  //3
17, //4
5,  //5
27, //6
4,  //7
20, //8
13, //9
};
int [] mathQuizRight = {
13, //0
12, //1
4,  //2
18, //3
4,  //4
4,  //5
9,  //6
7,  //7
4,  //8
4,  //9
};

Scanner scanner = new Scanner(System.in);
double userAns = 0;
double answer = 6;
System.out.println("Welcome to the 3rd Grade Math Quiz. n");
System.out.println("The Quiz begins now: n"
+ "1. 8 - 13n2. 14 + 12n3. 16 / 4n4. 8 + 18n5. 17 % 4n6. 5 x 4n"
+ "7. 27 / 9n8. 4 - 7n9. 20 x 4n10. 13 - 4");
for(int i = 0; i <= 10; i++) {
if (i < 10) {
System.out.printf("%d. ", (i + 1));
userAns = Double.valueOf(scanner.nextDouble());
add(mathQuizLeft, mathQuizRight, i, answer, userAns);
}
else {
add(mathQuizLeft, mathQuizRight, i, answer, userAns);
}
}
}

public static void add(int[] array1, int[] array2, int i, double answer, double userAns) {
ArrayList<Integer> mathAnswers = new ArrayList<>(); //for storing functions
int score = 0;
if (i == 1 || i == 3) { //addition
answer = array1[i] + array2[i];
if (userAns == answer) {
score++;
mathAnswers.add(i);
}
} else if (i == 0 || i == 7 || i == 9) { //subtraction
answer = array1[i] - array2[i];
if (userAns == answer) {
score++;
mathAnswers.add(i);
}
} else if (i == 5 || i == 8) { //multiplication
answer = array1[i] - array2[i];
if (userAns == answer) {
score++;
mathAnswers.add(i);
}
} else if (i == 2 || i == 6) { //multiplication
answer = array1[i] - array2[i];
if (userAns == answer) {
score++;
mathAnswers.add(i);
}
} else if (i == 4) { //multiplication
answer = array1[i] - array2[i];
if (userAns == answer) {
score++;
mathAnswers.add(i);
}
} else if (i == 10) {
for (double array: mathAnswers) {
System.out.println(array);
}
System.out.println(score);
}
}
}

您的错误在于add((方法。你在每个答对后计算分数,并将i添加到数组列表中,但你不能确保数组列表和分数的安全。您必须声明ArrayList和score为全局的,或者必须返回并保存在main((-方法中。

public class Program7 {
private static int score = 0;
private static ArrayList<Integer> mathAnswers = new ArrayList<>();
public static void main(String[] args) {
...
}
public static void add(...) {
if(...) {
...
}
...
}
}

我能够解决我的问题。我可能最终会删除这个,但我不得不大量使用这个方法来计算是什么让它递增并将值添加到数组中。我最终使用了ArrayList来添加它的值,而不是使用基元数组。我最终改写了整个方法,但最终还是让它发挥了作用。

import java.util.Scanner;
import java.util.Random;
import java.util.ArrayList;
public class Program7 {
public static void main(String[] args) {
int [] mathQuizLeft = {
8,  //0
14, //1
16, //2
8,  //3
17, //4
5,  //5
27, //6
4,  //7
20, //8
13, //9
};
int [] mathQuizRight = {
13, //0
12, //1
4,  //2
18, //3
4,  //4
4,  //5
9,  //6
7,  //7
4,  //8
4,  //9
};
int score = 0;
int userAns = 0;
int answer = 0;
System.out.println("Welcome to the 3rd Grade Math Quiz. n");
System.out.println("The Quiz begins now: n"
+ "1. 8 - 13n2. 14 + 12n3. 16 / 4n4. 8 + 18n5. Remainder of 17 / 4n6. 5 x 4n"
+ "7. 27 / 9n8. 4 - 7n9. 20 x 4n10. 13 - 4");
add(mathQuizLeft, mathQuizRight, answer, score, userAns);
}
public static void add(int[] array1, int[] array2, int answer, int score, int userAns) {
ArrayList<Integer> mathAnswers = new ArrayList<>(); //for storing functions
Scanner scanner = new Scanner(System.in);
answer = 0;
score = 0;
for(int i = 0; i < 10; i++) {
if(i == 10) {
break;
}
if (i == 1 || i == 3) { //addition
answer = array1[i] + array2[i];
} else if (i == 0 || i == 7 || i == 9) { //subtraction
answer = array1[i] - array2[i];
} else if (i == 5 || i == 8) { //multiplication
answer = array1[i] * array2[i];
} else if (i == 2 || i == 6) { //division
answer = array1[i] / array2[i];
} else { //modulo
answer = array1[i] % array2[i];
}
System.out.printf("%d. ", (i + 1));
userAns = Integer.valueOf(scanner.nextInt());
mathAnswers.add(answer);
if (userAns == answer) {
score++;
}
System.out.println(score);
System.out.println(mathAnswers.get(i));
}
if (score >= 7) {
System.out.println("Your score was: " + score + " - You passed!");
}
else {
System.out.println("Your score was: " + score + " - Please Try again.");
}
}
}

最新更新