基本上,我总体上想做的是:

您的一位教授听说了您新兴的编程专业知识,并要求您写单
可以用来帮助他们评分的程序。教授进行了三项50分的考试,
一次100分的期末考试。您的程序将提示用户输入学生姓名,输入为
名姓(即鲍勃·史密斯(Bob Smith)),学生的3考试成绩和1最终考试成绩(全部
数字)。班级规模因学期而异,但上限为100(声明为常数)。

在进行任何计算或显示任何输出之前,请阅读所有学生的信息。校验
输入的3项考试分数在0-50分之间,而最终分数在0-100分之间。
声明的最小值和最大值为常数,以便可以根据需要轻松地对其进行更新。如果无效,
显示错误消息,并允许用户重新输入该无效分数。阅读所有学生信息后
在其中,以LASTNAME,FIRSTNAME(所有大写字母)的格式显示每个学生的姓名,
考试百分比(所有考试的总和加上最终/可能的总数),精确到小数点后1位和学生的最终成绩。

但是我很难弄清楚如何将用户输入分配到一个数组中(或者是两个数组,可能是因为名字和姓氏?),但是我对要做的事情很迷茫,这就是我现在所拥有的:

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 = 0; i < exams.length; i++){
            if(i==3){
                System.out.print("Please enter score for Final Exam: ");
                exams[i] = s.nextInt();
            }
            else{
            System.out.print("Please enter score for Exam " + (i+1) + ": ");
            exams[i] = s.nextInt();

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


如果我的代码也有其他问题,那么对它的帮助也会很棒。

最佳答案

首先,声明需求指定的常量:

final int MAX_STUDENTS = 100;
final int MIN_EXAM = 0;
final int MAX_EXAM = 50;
final int MIN_FINAL = 0;
final int MAX_FINAL = 100.


然后,由于只允许使用数组,因此声明两个数组。一个将保留学生姓名,另一个将保留考试成绩。因此,它们将具有不同的类型。将String数组初始化为最大学生数。将考试分数数组初始化为4 * MAX_STUDENTS,因为每个学生将获得4考试分数:

String[] student_names = new String[MAX_STUDENTS];
int[] exam_scores = new int[MAX_STUDENTS * 4];


当您读入一个新学生时,将其姓名放入student_names数组的新索引中。然后,当您读完每个后续测试分数(3项考试,1项最终分数)时,逐渐填充exam_scores数组。

当您打印出分数时,请保留一个变量,以跟踪所打印的榜单数组的最后一个索引。这样,当您转到另一个学生时(在遍历数组时),您会知道在exam_scores数组中离开的地方。

还有其他(更好的)方法可以做到这一点(例如,使用列表是最好的方法,但是即使使用数组,也可能比这更好)。我不确定您到目前为止已学到了什么,因此选择了最基本的方法来实施该程序。

09-07 19:55
查看更多