Why is my counter not counting and my ArrayList not adding in the new integer values into the array?

huangapple 未分类评论46阅读模式
英文:

Why is my counter not counting and my ArrayList not adding in the new integer values into the array?

问题

以下是您提供的代码的翻译:

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("欢迎参加三年级数学测验。\n");
        System.out.println("测验现在开始:\n"
                + "1. 8 - 13\n2. 14 + 12\n3. 16 / 4\n4. 8 + 18\n5. 17 % 4\n6. 5 x 4\n"
                + "7. 27 / 9\n8. 4 - 7\n9. 20 x 4\n10. 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<>(); // 用于存储函数结果的数组

        int score = 0;

        if (i == 1 || i == 3) { // 加法
            answer = array1[i] + array2[i];
            if (userAns == answer) {
                score++;
                mathAnswers.add(i);
            }
        } else if (i == 0 || i == 7 || i == 9) { // 减法
            answer = array1[i] - array2[i];
            if (userAns == answer) {
                score++;
                mathAnswers.add(i);
            }
        } else if (i == 5 || i == 8) { // 乘法
            answer = array1[i] * array2[i];
            if (userAns == answer) {
                score++;
                mathAnswers.add(i);
            }
        } else if (i == 2 || i == 6) { // 除法
            answer = (double) array1[i] / array2[i];
            if (userAns == answer) {
                score++;
                mathAnswers.add(i);
            }
        } else if (i == 4) { // 取余
            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);
        }
    }
}

请注意,我已经根据原始代码进行了翻译。如果您对翻译有任何疑问,请随时提问。

英文:

The primary source of my issues is coming from my method "add" and the array "mathAnswers" and the counter "score."

I am trying to compare array values, make them perform a mathematical function, and then check to see if the user input matches the answer. I also want the correct answer to be added to an array, and then print out that array, along with the counter verifying the users score.

I'm running into an issue where my counter isn't incrementing, my values aren't inputting into my array, and everything remains 0. Here is my code.

    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(&quot;Welcome to the 3rd Grade Math Quiz. \n&quot;);
		System.out.println(&quot;The Quiz begins now: \n&quot;
				+ &quot;1. 8 - 13\n2. 14 + 12\n3. 16 / 4\n4. 8 + 18\n5. 17 % 4\n6. 5 x 4\n&quot;
				+ &quot;7. 27 / 9\n8. 4 - 7\n9. 20 x 4\n10. 13 - 4&quot;);

		for(int i = 0; i &lt;= 10; i++) {
			if (i &lt; 10) {
				System.out.printf(&quot;%d. &quot;, (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&lt;Integer&gt; mathAnswers = new ArrayList&lt;&gt;(); //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);
		}
	}
}

答案1

得分: 0

你的错误在add()方法中。你在每个正确答案后都计算分数并将i添加到ArrayList中,但你没有保存ArrayList和分数。你必须将ArrayList和分数声明为全局变量,或者你必须将其返回并在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(...) {
      ...
    }
    ...
  }
}
英文:

Your misstake is in the add() method. You count after each right answer the score and add i to the ArrayList, but you doesn't safe the arraylist and the score. You have to declare the ArrayList and the score global or you have to return it and save it in the main()-method.

public class Program7 {
  private static int score = 0;
  private static ArrayList&lt;Integer&gt; mathAnswers = new ArrayList&lt;&gt;();

  public static void main(String[] args) {
    ...
  }

  public static void add(...) {
    if(...) {
      ...
    }
    ...
  }
}

答案2

得分: 0

我成功解决了我的问题。我可能会最终删除它,但我不得不反复尝试这个方法,以找出如何使其递增并将值添加到数组中。最终我使用了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 - 13\n2. 14 + 12\n3. 16 / 4\n4. 8 + 18\n5. Remainder of 17 / 4\n6. 5 x 4\n"
                + "7. 27 / 9\n8. 4 - 7\n9. 20 x 4\n10. 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.");
        }
    }
}
英文:

I was able to solve my issue. I may just end up deleting this but I had to play with the method a lot to figure out what would get it to increment and add the value to the array. I ended up using an ArrayList to add the values for it instead of a primitive array. I ended up rewriting the whole method but I ended up getting it to work.

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(&quot;Welcome to the 3rd Grade Math Quiz. \n&quot;);
		System.out.println(&quot;The Quiz begins now: \n&quot;
				+ &quot;1. 8 - 13\n2. 14 + 12\n3. 16 / 4\n4. 8 + 18\n5. Remainder of 17 / 4\n6. 5 x 4\n&quot;
				+ &quot;7. 27 / 9\n8. 4 - 7\n9. 20 x 4\n10. 13 - 4&quot;);

		add(mathQuizLeft, mathQuizRight, answer, score, userAns);

	}

	public static void add(int[] array1, int[] array2, int answer, int score, int userAns) {
		ArrayList&lt;Integer&gt; mathAnswers = new ArrayList&lt;&gt;(); //for storing functions
		Scanner scanner = new Scanner(System.in);

		answer = 0;
		score = 0;

		for(int i = 0; i &lt; 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(&quot;%d. &quot;, (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 &gt;= 7) {
			System.out.println(&quot;Your score was: &quot; + score + &quot; - You passed!&quot;);
		}
		else {
			System.out.println(&quot;Your score was: &quot; + score + &quot; - Please Try again.&quot;);
		}
	}
}

huangapple
  • 本文由 发表于 2020年4月7日 07:31:02
  • 转载请务必保留本文链接:https://java.coder-hub.com/61070525.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定