java - 混淆for循环

标签 java arrays for-loop

每当我的程序到达此for循环并尝试运行它时,我都会报错

for (int m = 0; m < students; m++){
        allScores = allScores + scores[m];


学生被宣布为int
allScores与scores []数组一样声明为double。

这是错误消息:

线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:2
    在Lab4.main(Lab4.java:51)

第51行是for(int m = 0; m
编辑:

这是我的整个代码,我可能未正确完成scores []数组:

import java.util.*;
import java.text.*;

public class Lab4 {
public static void main(String[] args){
    Scanner s= new Scanner(System.in);
    String input;
    int students;
    int d=1;
    double allScores = 0;

    char [] answerKey= { 'B' , 'D' , 'A' , 'A' , 'C' , 'A' , 'B' , 'A' , 'C' , 'D' , 'B' , 'A' };
    char [] userAnswers = new char[answerKey.length];

    DecimalFormat df = new DecimalFormat("#0.0");

    System.out.print("how many students are in the class? ");
    input = s.nextLine();
    students=Integer.parseInt(input);

    String [] name = new String[students];
    double [] scores = new double[students];

    for (int j = 0; j < students; ++j)
    {
        int correctAnswers=0;

        System.out.print("Enter name of student " + (j + 1) + ": ");
        name[j] = s.nextLine();

        System.out.print("Enter quiz score answers :");
        String line = s.nextLine().toUpperCase();
        for (int k = 0; k < answerKey.length; k++) {
        userAnswers[k] = line.charAt(k);
            }


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

                if(userAnswers[i] == answerKey[i])
                {
                    correctAnswers++;
                }



        }
            System.out.println(name[j]);
            System.out.println((df.format((((float)(correctAnswers) / answerKey.length)) * 100)) + "%");
            scores [d] = ((float)((correctAnswers) / answerKey.length) * 100);
            d++;
    }
    System.out.println("the high score is "   + "and the low score is "   );
    for (int m = 0; m < scores.length; m++){
        allScores = allScores + scores[m];
    }
    System.out.println("Average is:" + ((allScores/students)));

}

}


编辑:

对不起,我读错了51行

scores [d] = ((float)((correctAnswers) / answerKey.length) * 100);


编辑:

新代码不再给我错误消息,但是我似乎无法获得平均分数。

import java.util.*;
import java.text.*;

public class Lab4 {
public static void main(String[] args){
    Scanner s= new Scanner(System.in);
    String input;
    int students;
    int d=1;
    double allScores = 0;

    char [] answerKey= { 'B' , 'D' , 'A' , 'A' , 'C' , 'A' , 'B' , 'A' , 'C' , 'D' , 'B' , 'A' };
    char [] userAnswers = new char[answerKey.length];

    DecimalFormat df = new DecimalFormat("#0.0");

    System.out.print("how many students are in the class? ");
    input = s.nextLine();
    students=Integer.parseInt(input);

    String [] name = new String[students];
    double [] scores = new double[students + 1];

    for (int j = 0; j < students; ++j)
    {
        int correctAnswers=0;

        System.out.print("Enter name of student " + (j + 1) + ": ");
        name[j] = s.nextLine();

        System.out.print("Enter quiz score answers :");
        String line = s.nextLine().toUpperCase();
        for (int k = 0; k < answerKey.length; k++) {
        userAnswers[k] = line.charAt(k);
            }


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

                if(userAnswers[i] == answerKey[i])
                {
                    correctAnswers++;
                }



        }
            System.out.println(name[j]);
            System.out.println((df.format((((float)(correctAnswers) / answerKey.length)) * 100)) + "%");
            scores [d] = ((float)((correctAnswers) / answerKey.length) * 100);
            d++;
    }
    System.out.println("the high score is "   + "and the low score is "   );
    for (int m = 0; m < scores.length; m++){
        allScores = allScores + scores[m];
    }
    System.out.println("Average is:" + ((allScores/students)));

}

}

最佳答案

在数组中使用length属性进行迭代。

for (int m = 0; m < scores.length; m++){
        allScores = allScores + scores[m];


但是,您提到错误发生在第51行,但第51行是:

scores [d] = ((float)((correctAnswers) / answerKey.length) * 100);


因此,您的d等于或大于scores.length。

我很好奇您为什么将d设为1?

将其更改为0可能会解决问题。

int d=0;


您的代码中的另一个问题是:

((float)((correctAnswers) / answerKey.length) * 100);


更改为

(((float)correctAnswers / answerKey.length) * 100);


否则,您将更改结果,该结果已经是要浮点数的int。那会影响结果。

关于java - 混淆for循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15104116/

相关文章:

java - 如何使用 Java 从两个 for 循环中获取单独的输出

java - Camera Intent 不会将图像保存到图库

javascript - 为什么 onClick 对我的按钮不起作用?

arrays - 将函数应用于 Julia 中的列对

c - 试图让这个 for 循环工作?

java - JTextArea行数限制

Java - 以编程方式获取与域名关联的每个网页

c++ - 在C++中,当一个数组的元素在一个循环中被多次使用时,是不是把它赋值给另一个元素更好?

java - 将 do-while 循环转换为 Java 中的 for 循环

c - 为什么这个for循环不执行?