gpt4 book ai didi

java - 正确调用方法: (Java)

转载 作者:行者123 更新时间:2023-12-03 00:51:29 25 4
gpt4 key购买 nike

Create a method named getNextProjectIndex() that returns an int that gets the next index of the projects array that contains a -1.0. Find the next item in the projects array that contains a -1.0 and return that index of the array. This method should return -1 if the array is full. -1 is a common flag to indicate failure in methods that should only return non-negative integers.

我在主方法中调用此方法时遇到问题。问题在于我的 getNextProjectIndex 方法还是我的 main 方法如何调用它?

  public double getNextProjectIndex()
{
int i = 0;
int full = -1;
while(i < projects.length)
{
if (projects[i] == -1)
{

return i;
}
else if (i == (projects.length - 1) && projects[i] != -1)
{
return full;
}

}

return i;

}

这是我的主要方法:

public class Main {

public static void main(String [ ] args)
{
Student testStudent = new Student("BL", "Hill", 34);
int i = 0;
System.out.println(testStudent.getFname() + " " + testStudent.getLname() );

while(i < 5)
{
if(testStudent.getNextProjectIndex() != 0)
{
testStudent.setProjectScore(10.0, i);
System.out.println("Scores are: "+ testStudent.getProjectScore(i));
}


i++;
}


}

}

当程序运行时,仅显示 1 个分数,而不是 5 个分数。它没有完全贯穿循环。如果数组中的所有槽都已填满,则该方法只应返回 -1。

最佳答案

Create a method named getNextProjectIndex() that returns an int

您已将您的方法声明为返回 double,即使规范规定返回 int,并且您尝试在函数中返回 int i:

public double getNextProjectIndex()
^^^^^^

关于java - 正确调用方法: (Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9892548/

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