【发布时间】:2016-05-21 12:09:32
【问题描述】:
我正在尝试获取我拥有的代码输入并将它们转换为一个 int 数组列表,以便我可以使用它们来计算平均分数,但我不确定如何执行此操作。此外,当涉及到分数时,输入的范围可以从 1 到 5,因此一个学生可能有 3 个分数,下一个学生可能有 5 个分数,依此类推。这是我到目前为止的代码。
我的主要问题是如何获取列表输入并将它们转换为单独的数组整数?
//data declarations and initalizations
final int MAX_STUDENTS = 12;
final int MAX_SCORE = 5;
//data declarations
double avg; //average total for all students
int numStudents; //number of students 1-12
double totalAvgStu = 0.0; //
int num;
String stuScores = " "; //student score 1-5
//array declarations and inistiations
int [] score = new int[MAX_SCORE]; // score from input range between 1-5
double [] avgScores = new double[MAX_STUDENTS]; //avg total of score for one student
String [] testScores = new String[MAX_STUDENTS];
String [] array1 = new String[MAX_SCORE];
//user inputs for number of students
numStudents = UtilsKS.readInt("Enter number of students: ", false);
//while loop for correct input of students
while (numStudents <= 0 || numStudents > 12) {
System.out.print("ERROR: must be 1-12, ");
numStudents = UtilsKS.readInt("Enter number of students: ", false);
}
// user input for student score using a for loop
for (int i = 0; i < numStudents; i++) {
stuScores = UtilsKS.readString("Enter comma-separated test score for student " + (i+1) + ": ", false);
}
//for loop for arrays
for (int i=0; i<avgScores.length; i++) {
totalAvgStu += avgScores[i];
}
//outputs
String header = "average score1 score2 score3 score4 score5";
System.out.println("\n" + header);
System.out.println("\nAvg for all students = " + totalAvgStu/avgScores.length);
//initializing testscore to = what scores are inputed per student
for (int t=0; t<numStudents; t++) {
testScores[t] = stuScores;
}
【问题讨论】:
-
您已发布要求和代码,但没有提出具体问题。我们当然不会为您编写代码,但我们可以回答问题。所以请解决这个问题,以便我们知道如何为您提供帮助。
-
这里有很多代码。究竟是什么问题?
-
对不起,我添加了我的主要问题,我是新手。我知道你必须以某种方式使用 .split() 和 integer.parseInt() 但我不明白如何?
-
另请查看this Google search,了解本网站上的其他类似问题和答案。
-
感谢那些真正提供帮助的人!