我正在学习Java,我正在使用BlueJ编写一个返回currentTimeInMillis()的方法,而y< 100.目前我收到一条错误,上面写着“缺少退货声明”。任何建议都是错误/代码吗?
import java.lang.System;
public class Math
{
// instance variables - replace the example below with your own
private int y;
/**
* Constructor for objects of class Math
*/
public Math()
{
// initialise instance variables
y = 0;
}
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public static long currentTimeMillis()
{
// put your code here
for (int y = 0; y<100; y++)
{return y;
}
System.out.println(System.currentTimeMillis());
}
}
答案 0 :(得分:0)
你的currentTimeMillis
应该返回很长时间。这个签名
public static long currentTimeMillis()
声明您的私有,静态方法必须返回 long 类型的值。
您,编码人员可以假设for循环将始终执行,但编译器无法执行,这就是您必须在方法结束时添加return
语句的原因。我虽然重构了整个方法......
答案 1 :(得分:0)
您需要做的就是最后添加一个return语句:
public static long currentTimeMillis()
{
// put your code here
for (int y = 0; y<100; y++)
{
return y; // This code does not make sense as it will always return 0
}
System.out.println(System.currentTimeMillis());
// from the function name it appears you want to return current time millis
return System.currentTimeMillis();
}