提问者:小点点

获得学生评分考试成绩的平均值


在进行任何计算或显示任何输出之前,读取所有学生的信息。在输入时验证3个考试成绩在0-50分之间,期末考试成绩在0-100分之间。声明最小值和最大值为常数,以便根据需要轻松更新。如果无效,请显示错误消息并允许用户重新输入无效分数。读入所有学生信息后,以LASTNAME、FIRSTNAME(全部大写)、学生考试百分比(所有考试的总数加上期末/可能的总分)至小数点后1位以及学生的期末成绩的格式显示每个学生的姓名,基于以下百分比:

90-100 A,80-89.9 B,70-79.9 C,60-60.9 D,低于60 F

这是我已经输入的,但它给了我一些错误,我不确定我是否正确分配了值。

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


public class Proj4 {
public static void main(String[] args){
Scanner s= new Scanner(System.in);
String input;
String again = "y";

int [] exams = new int[4];
int student = 1;

do
{

    String [] names = new String[student];
        System.out.print("PLease enter the name of student" + student );
        names[student-1] = s.nextLine();
        for ( int i = 1; i < exams.length; i++){
            if(i==4){
                System.out.print("Please enter score for Final Exam: ");
            }

            System.out.print("Please enter score for Exam" + i);
            exams[i] = s.nextInt(); 

                if((exams[1]<0||exams[1]>50)||(exams[2]<0||exams[2]>50)||(exams[3]<0||exams[3]>50)){
                    System.out.println("Invalid enter 0-50 only...");
                    System.out.print("Please re-enter score: ");
                    exams[i] = s.nextInt();
                }
                else if(exams[4]<0||exams[4]>100){
                    System.out.println("Invalid enter 0-100 only...");
                    System.out.print("Please re-enter score: ");
                    exams[i] = s.nextInt();
                }
        }
        System.out.println("do you wish to enter another?");
        again = s.nextLine();
    student++;
}while (again.equalsIgnoreCase ("y"));
}
}

这是错误消息:

线程“main”中的异常java. lang.ArrayIndexOutOfBoundsException:4 at Proj4.main(Proj4.java:32)

但它令人困惑,因为第32行只是一个},所以我真的不知道它到底是什么意思,我知道我可能需要减少数组的一个输入的长度,但我不确定哪个或是否有更多的错误。

编辑:

对不起,这是我的问题,你们能帮我找出问题和/或告诉我如果或我做错了什么,以获得所需的输出吗?


共2个答案

匿名用户

数组考试只有4个元素,但您尝试在这里使用索引4访问第5个元素考试[4]

匿名用户

下面的方法适合你吗?

public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        String again = "y";

        int[] exams = new int[4];
        int student = 1;

        do {

            String[] names = new String[student];
            System.out.print("PLease enter the name of student" + student);
            names[student - 1] = s.nextLine();
            for (int i = 0; i < exams.length; i++) {
                System.out.println(i + " i");
                if (i == 3) {
                    System.out.print("Please enter score for Final Exam: ");
                    exams[i] = s.nextInt();
                    if (exams[3] < 0 || exams[3] > 100) {
                        System.out.println("Invalid enter 0-100 only...");
                        System.out.print("Please re-enter score: ");
                        exams[i] = s.nextInt();
                    }
                } else {
                    System.out.print("Please enter score for Exam " + i);
                    exams[i] = s.nextInt();

                    if ((exams[i] < 0 || exams[i] > 50)) {
                        System.out.println("Invalid enter 0-50 only...");
                        System.out.print("Please re-enter score: ");
                        exams[i] = s.nextInt();
                    }

                }
            }
            System.out.println("do you wish to enter another?");
                again = s.next();
                student++;
        } while (again.equalsIgnoreCase("y"));
    }