我正在创建一个类,以保留学生参加的测验分数。这是规格:构造函数是a。使用输入参数的长度创建一个 int 类型数组的实例。数组中的每个元素都初始化为 -1。湾。计数设置为0。它不是数组的长度,而是有效分数的数量。换句话说,scores 是一个部分填充的数组,count 作为有效分数的 END 位置。C。使用第二个输入参数设置名称。
我在使用 add 方法时遇到问题。我必须输入学生将参加多少次测验的大小,然后将他们参加的每个测验的分数相加,直到数组填满。
示例:如果我输入测验的大小为 3,我将能够添加 3 个不同的分数,但如果我超过 3,它必须显示一条消息,例如“数组已满。无法添加值 _____” .
这是我到目前为止的课程:
public class Quiz {
private static int [] scores;
private int count;
private static String name;
public Quiz (int[] scores, int count, String name){
int size=0;
scores=new int [size];
count=0;
name=" ";
}
public void addScores( int [] scores){
int size=0;
scores=new int [size];
for(int i=0; i<size;i++)
if(i<size)
System.out.println(scores[i]);
else
System.out.println("Array is full! the value"+ " " + scores + " "+ "cannot be added.");
}
这是测试驱动程序代码的一部分:
Scanner in = new Scanner (System.in);
do {
System.out.println("\nPlease enter a command or type ?");
String choice;
choice = in.next().toLowerCase();
command = choice.charAt(0);
switch (command) {
case 'n':
System.out.println("[Create a new data]");
System.out.println("[Input the size of quizzes]:"+ " ");
int size=in.nextInt();
String linebreak = in.nextLine();
System.out.println("[Input the name of student]:"+ " ");
String name=in.nextLine();
break;
case 'a':
System.out.println("a [Add a score]:"+ " ");{
int i=0;
i=in.nextInt();
break;
}
}