字符数组无法正确读取输入

泰勒

我的任务是制作一个具有一个char数组的程序,该数组填充了10个问题测试的正确答案。我制作了一个数组,其中填充了用户答案,并且应该检查用户答案和测试答案,并在给出用户输入的情况下给出通过或失败的输出。代码会编译,我可以输入10个字符,但是无论我输入什么字符,输出总是10个答案,其中10个是不正确的。

在过去的几个小时中,我一直感到困惑,试图找出答案,并希望在这里有所帮助。

这是代码片段:

     //Part 2
    char[] correctAnswers = {'b', 'd', 'a', 'a', 'c', 'a', 'b', 'a', 'c', 'd'}; //Char arrays
    char[] studentAnswers = new char[10];

    System.out.println("What are the students 10 answers?"); //Getting student answers

    for (int i = 0; i < correctAnswers.length; i++)
        studentAnswers = scan.next().toCharArray();

    int points = 0; //Used to calculate pass or fail

    for (int i = 0; i < correctAnswers.length; i++);

    int j = 0; //Used to identify each element in the array

    if (correctAnswers[j] == studentAnswers[j]) //Checks each answer with the correct answers and adds 1 point if it is true
    {
        points++;
        j++;
    }
    else {
        j++;
    }

     if (points >= 8) {
        System.out.println("Congratulations! \nYou have passed exam.");
        System.out.println("Total number of correct answers: " + points); //print points
        System.out.println("Total number of incorrect answers: " + (correctAnswers.length - points)); //10 - points would equal the remaining amount of points available which would be how many were missed.
    } else {
        System.out.println("Sorry, you have not passed the exam!");
        System.out.println("Total number of correct answers: " + points);
        System.out.println("Total number of incorrect answers: " + (correctAnswers.length - points));
    }
马拉卡
for(int i = 0; i < correctAnswers.length; i++)
    studentAnswers = scan.next().toCharArray();
  1. 我认为您应该删除第一行。它出现两次。假设您读完所有10个答案作为1个字符串。

  2. 然后使用i无处不在,而不是j,它是多余的(删除的声明jj++,因此else成为空可也删除)。

  3. 另外,你需要把牙套{}for,输出前(即检查的总积分前)。如果您;在后面加上分号for则表示您无事可做x次。

我明白了您为什么使用j:因为i未定义。它只存在于循环中(由空,跳过或“什么都不做”命令组成:一个孤独的分号)。当您添加{}(请参阅3.)时,它们就i位于这些花括号之间。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章