所以在学校,我们刚刚学习了数组,我们有一个项目。它是对学生姓名,测试平均分,测验平均分,家庭作业平均分和最终平均分进行并行排列。此外,我们必须找到最高和最低分。除非有2名学生在同一所高中上学,否则我的得分最高,那么它只会显示一个。以下是我的方法:
public String getHighestScore(){
double highScore = 0;
String students = "";
for(int i = 0; i < totalTests; i++){
if(quarterAverages[i] > highScore){
highScore = quarterAverages[i];
students = names[i];
}
}
return students;
}
这段代码现在有效。我需要在if语句中使大于或等于大于或等于并为字符串添加名称而不是将其更改为一个名称。
public String getHighestScore(){
double highScore = 0;
String students = "";
for(int i = 0; i <= totalTests; i++){
if(quarterAverages[i] >= highScore){
highScore = quarterAverages[i];
students += names[i] + " ";
}
}
return students;
}
这个只输出1个高分而不是2分的学生。我的问题是如何输出具有相同高分的多个学生。
答案 0 :(得分:2)
要返回多个名称,您需要做一些事情:
List<String>
而不是单个String
值。 (当然,如果最高分没有联系,那么该列表将只有一个元素。)i
th 分数是否与当前的高分相关联,并将名称添加到当前列表中。这样的事情(未经测试)可能会起到作用:
public List<String> getHighestScore() {
double highScore = 0;
List<String> students = new ArrayList<>();
for(int i = 0; i < totalTests; i++){
if(quarterAverages[i] >= highScore){
if (quarterAverages[i] > highScore) {
highScore = quarterAverages[i];
students.clear();
}
students.add(names[i]);
}
}
return students;
}
编辑:由于您只是在学习数组,因此您可能不了解Java的List
数据结构。这是一个变体,它返回所有名称的串联,用分隔符分隔:
public String getHighestScore() {
double highScore = 0;
String students = "";
String delimiter = ", ";
for(int i = 0; i < totalTests; i++){
if(quarterAverages[i] >= highScore){
if (quarterAverages[i] > highScore) {
highScore = quarterAverages[i];
students = "";
} else if (students.length() > 0) {
students += delimiter;
}
students += names[i];
}
}
return students;
}
(当然,这应该用StringBuilder
来完成,但我不知道你已经了解了这一点。)
P.S。由于此方法返回得分最高的名称,而不是最高得分,我强烈建议您相应地重命名该方法。
答案 1 :(得分:0)
Buddy,鉴于您提供的信息和代码数量,以下是我能提出的最佳建议。想要发送学生列表而不是包含一个名字的字符串。
public ArrayList<String> getHighestScore(){
double highScore = 0;
ArrayList<String> students = new ArrayList<String>();
for(int i = 0; i < totalTests; i++){
if(quarterAverages[i] > highScore){
highScore = quarterAverages[i];
students.add(names[i]);
}
}
return students;
}
答案 2 :(得分:0)
首先找到最高分,然后找到得分最高的学生。
import os
import pexpect # $ pip install pexpect
hw = '/absolute/path/to/parent/directory/hw'
child = pexpect.spawn(hw, cwd=os.path.dirname(hw))
# ...
或者你可以只使用一个循环
public static String getHighestScore() {
// First get the highest score
double highScore = 0;
for (int i = 0; i < quarterAverages.length; i++) {
if (quarterAverages[i] > highScore) {
highScore = quarterAverages[i];
}
}
// Now get the students with the highest score
String students = "";
for (int i = 0; i < quarterAverages.length; i++) {
if (quarterAverages[i] == highScore) {
students += names[i] + ", ";
}
}
// Substring is to remove the ", " at the end. That's what the -2 does.
return students.substring(0, students.length() - 2);
}
数据
public static String getHighestScore() {
// First get the highest score
double highScore = 0;
String students = "";
for (int i = 0; i < quarterAverages.length; i++) {
if (quarterAverages[i] >= highScore) {
if (quarterAverages[i] > highScore) {
highScore = quarterAverages[i];
students = "";
}
students += names[i] + ", ";
}
}
// Substring is to remove the ", " at the end. That's what the -2 does.
return students.substring(0, students.length() - 2);
}
结果:
private String[] names = { "Chris", "John", "Joe" };
private double[] quarterAverages = { 100.0, 100.0, 99.0 };
答案 3 :(得分:0)
好的,所以我会尽我所能帮助你提供你给我们的信息。 考虑到它是学校的分配,我想你必须返回一个字符串,不允许使用列表等等。
我还假设以下内容:
public String getHighestScore(){
double highScore = 0;
String students = "";
for(int i = 0; i <= totalTests; i++){
if(quarterAverages[i] >= highScore){
highScore = quarterAverages[i];
students += names[i] + " ";
}
}
return students;
}
包含学生姓名。让我们看看您的第一个代码段(不是我建议的解决方案)
public String getHighestScore(){
double highScore = -1; //maybe the highest score is 0
String students = "";
for(int i = 0; i <= totalTests; i++){
if(quarterAverages[i] > highScore){
highScore = quarterAverages[i];
students = names[i];
}
else if(quarterAverages[i] == highScore){
students += ", " + names[i];
}
}
return students;
}
所以,让我们说,现在有一名学生在指数0处的quaterAverage为5,而在指数1处的quaterAverage为6的学生。 如果您在当前季度平均值较高时在字符串末尾添加一些内容,则您将获得学生在索引0处的名称(当前是最高分数),然后是索引1处的学生姓名(然后是最高分数) )。这不是你想要的,因为你只想要得分最高的人的名字。
我会提出这样的建议:
$(".comp_id").on("click",function(){
var compID=$(this).val();
$("#pending").load("/pages/tipping/add-tips.php?pending=1&tip_comp="+compID);
});
现在,每当我们找到一个与当前最高分相同分数的学生时,我们会将名称添加到String中。如果我们找到一个新的最高分,我们重置字符串并从那里开始。