gpt4 book ai didi

java - 在java中用double返回一个对象?

转载 作者:行者123 更新时间:2023-12-02 05:36:27 25 4
gpt4 key购买 nike

我有一个非常简单的解决方案,但我还没有学会如何做,也不知道如何解决问题。

我有一个接口(interface)类:

public interface Analyzable { 
double getAverage();
GradedActivity getHighest();
GradedActivity getLowest();
}

这是我的教授给我的。我将他的代码从两个 GradedActivity 更改为 double ,并且在此代码中返回它们没有问题。但是我无法将它们作为 GradedActivity 的

public GradedActivity getHighest() {
double highest = grades[0].getScore();
for(int i = )......
//Code works and returns correct number (tested)
return highest;

这段代码给了我这个错误“Double 无法转换为 GradedActivity”

这是我的 GradedActivity 代码

public class GradedActivity { 
private double score;

public GradedActivity() {
}

public void setScore(double s) {
score = s;
}

public double getScore() {
return score;
}

public char getGrade() {
char letterGrade;
if(score >= 90)
letterGrade = 'A';
else if(score >= 80)
letterGrade = 'B';
else if(score >= 70)
letterGrade = 'C';
else if(score >= 60)
letterGrade = 'D';
else
letterGrade = 'F';

return letterGrade;
}

}

这是 CourseGrades 代码

enter code here

公共(public)类 CourseGrades 扩展了 GradedActivity 实现了 Analyzable{ 私有(private)GradedActivity[]成绩=新GradedActivity[4];

public CourseGrades()
{

}

public void setLab(GradedActivity lab)
{
grades[0] = lab;
}

public void setPassFailExam(PassFailExam passFailExam)
{
grades[1] = passFailExam;
}

public void setEssay(GradedActivity essay)
{
grades[2] = essay;
}

public void setFinalExam(FinalExam finalExam)
{
grades[3] = finalExam;
}



public String toString() {
String str;
str = "Lab Score: " + grades[0].getScore() +
"\t\t\tGrade: " + grades[0].getGrade() +
"\nP/F Exam Score: " + grades[1].getScore() +
"\t\tGrade: " + grades[1].getGrade() +
"\nEssay Score: " + grades[2].getScore() +
"\t\tGrade: " + grades[2].getGrade() +
"\nFinal Exam Score: " + grades[3].getScore() +
"\t\tGrade: " + grades[3].getGrade();
return str;
}

public double getAverage()
{
double sum = 0;
for(int i = 0; i < 4; i++) {
sum += grades[i].getScore();
}
double ave = sum/4;
return ave;
}

public GradedActivity getHighest() {

double highest = grades[0].getScore();
for(int i = 1; i < 4; i++) {
if(grades[i].getScore() > highest)
highest = grades[i].getScore();
}
return highest;
}

public GradedActivity getLowest() {
double lowest = grades[0].getScore();
for(int i = 1; i < 4; i++) {
if(grades[i].getScore() < lowest)
lowest = grades[i].getScore();
}
return setScore(lowest);
}

}

这是界面

公共(public)类 CourseGrades 扩展了 GradedActivity 实现了 Analyzable{ 私有(private)GradedActivity[]成绩=新GradedActivity[4];

public CourseGrades()
{

}

public void setLab(GradedActivity lab)
{
grades[0] = lab;
}

public void setPassFailExam(PassFailExam passFailExam)
{
grades[1] = passFailExam;
}

public void setEssay(GradedActivity essay)
{
grades[2] = essay;
}

public void setFinalExam(FinalExam finalExam)
{
grades[3] = finalExam;
}



public String toString() {
String str;
str = "Lab Score: " + grades[0].getScore() +
"\t\t\tGrade: " + grades[0].getGrade() +
"\nP/F Exam Score: " + grades[1].getScore() +
"\t\tGrade: " + grades[1].getGrade() +
"\nEssay Score: " + grades[2].getScore() +
"\t\tGrade: " + grades[2].getGrade() +
"\nFinal Exam Score: " + grades[3].getScore() +
"\t\tGrade: " + grades[3].getGrade();
return str;
}

public double getAverage()
{
double sum = 0;
for(int i = 0; i < 4; i++) {
sum += grades[i].getScore();
}
double ave = sum/4;
return ave;
}

public GradedActivity getHighest() {

double highest = grades[0].getScore();
for(int i = 1; i < 4; i++) {
if(grades[i].getScore() > highest)
highest = grades[i].getScore();
}
return highest;
}

public GradedActivity getLowest() {
double lowest = grades[0].getScore();
for(int i = 1; i < 4; i++) {
if(grades[i].getScore() < lowest)
lowest = grades[i].getScore();
}
return setScore(lowest);
}

}

最佳答案

由于 double 无法转换为 GradedActivity,因此您需要先创建一个,设置值(我将创建一个构造函数),然后返回它。

尝试

   GradedActivity highGA = new GradedActivity();
highGA.setScore (highest);
return highGA;

关于java - 在java中用double返回一个对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24923910/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com