gpt4 book ai didi

java - 尝试为我的代码获取正确的值答案

转载 作者:行者123 更新时间:2023-12-02 06:40:40 24 4
gpt4 key购买 nike

下面是我为学生类(class)和主要方法编写的代码。我有两个问题。首先,当我尝试将 main 作为自己的类时,如果无法运行和编译,则表示存在错误,main 无法在 main 中引用和创建学生类。

第二个问题,打印出最高平均分的最后一行,总是打印出 0.0,我一辈子都无法弄清楚为什么。

有人可以给我解决这两个问题吗?

我正在使用 NetBeans。

package student;

public class Student {

private String name, id;
private int[] score = new int[3];
public Student()
{

}
public Student(String stName, String stID, int stScore[]) {
this.name = stName;
this.id = stID;
this.score = stScore;
}
public void setName(String nameIn)
{
name = nameIn;
}
public String getName()
{
return name;
}
public double avScore()
{
double total = 0;
int to = 0;
int adder = 0;
for (int i=0; i<score.length; i++)
{
score[i] = adder;
total = total + adder;
}
total = total / score.length;
return total;
}
public void printOut() {
System.out.println("Student Name is: " + name) ;
System.out.println("Student ID is: " + id);
System.out.println("Student scores are: ");
for (int i=0; i<score.length; i++)
{
System.out.println(score[i]);
}
}

public static void main(String args []) {
Student stud1 = new Student("Nico Del Pellegrino", "up660537", new int[] {1, 2, 3});
Student stud2 = new Student("Dylan Scott", "up652312", new int[] {5, 7, 13});
stud1.printOut();
stud2.printOut();
Student stud3 = new Student();
stud3.id = "up645658";
stud3.name = "Alex Barrett";
stud3.score = new int[]{5, 10, 15};
stud3.printOut();
double stud1Score = stud1.avScore();
double stud2Score = stud2.avScore();
double stud3Score = stud3.avScore();
double[] scoreList = {stud1Score, stud2Score, stud3Score};
double highestMark = 0;
for (int i=0; i<scoreList.length;)
{
if(scoreList[i]>highestMark)
{
highestMark = scoreList[i];
i++;
}
else
{
i++;
}
}
System.out.println("The highest average mark is: " + highestMark);

}
}

编辑:这是其单独类中的代码,以及运行 main 时出现的错误消息。

package student;

public class Student {

private String name, id;
private int[] score = new int[3];

public Student() {
}

public Student(String stName, String stID, int stScore[]) {
this.name = stName;
this.id = stID;
this.score = stScore;
}

public void setName(String nameIn) {
name = nameIn;
}

public String getName() {
return name;
}

public double avScore() {
double total = 0;
int to = 0;
for (int i = 0; i < score.length; i++) {
total = total + score[i];
}
total = total / score.length;
return total;
}

public void printOut() {
System.out.println("Student Name is: " + name);
System.out.println("Student ID is: " + id);
System.out.println("Student scores are: ");
for (int i = 0; i < score.length; i++) {
System.out.println(score[i]);
}
}
}


package Student;

import Student.*;

public class Main {

public static void main(String args []) {
//Create two student objects stud1 and stud2 here
Student stud1 = new Student("Nico Del Pellegrino", "up660537", new int[] {1, 2, 3});
Student stud2 = new Student("Dylan Scott", "up652312", new int[] {5, 7, 13});
//Display information for the two objects
stud1.printOut();
stud2.printOut();
//Create third student object stud3 here
Student stud3 = new Student();
// change object id
stud3.id = "up645658";
// change object name
stud3.name = "Alex Barrett";
// change object exam scores
stud3.score = new int[]{5, 10, 15};
stud3.printOut();
// Find out which student is with the highest average score
int stud1Score = stud1.avScore();
int stud2Score = stud2.avScore();
int stud3Score = stud3.avScore();
//Display his/her details here
}
}


//run:
//Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree //type: Student.Student
// at Student.Main.main(Main.java:9)
//Java Result: 1
//BUILD SUCCESSFUL (total time: 0 seconds)

最佳答案

您在此行的分配不正确:

score[i] = adder;

您将 adder 初始化为 0,因此您实际上在整个数组中放置了零。难怪平均值为 0.0。而不是

score[i] = adder;
total = total + adder;

你甚至不需要adder,只需使用:

total = total + score[i];

关于java - 尝试为我的代码获取正确的值答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19169134/

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