Java-for循环currentTimeInMillis()-BlueJ
目前我正在学习Java,我正在使用BlueJ编写一个方法,在y<100时返回currentTimeInMillis()。目前,我收到一个错误,声明“缺少返回语句”。关于错误/代码有什么建议吗Java-for循环currentTimeInMillis()-BlueJ,java,bluej,Java,Bluej,目前我正在学习Java,我正在使用BlueJ编写一个方法,在y
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());
}
}
导入java.lang.System;
公共课数学
{
//实例变量-将下面的示例替换为您自己的
私营企业;
/**
*数学类对象的构造函数
*/
公共数学
{
//初始化实例变量
y=0;
}
/**
*方法示例-将此注释替换为您自己的注释
*
*@param y方法的示例参数
*@返回x和y的总和
*/
公共静态长currentTimeMillis()
{
//把你的代码放在这里
对于(int y=0;y您的currentTimeMillis
应该返回一个long。此签名
public static long currentTimeMillis()
声明您的私有静态方法必须返回long类型的值
您,编码人员,可以假设for循环将始终执行,但编译器不能,这就是为什么您必须在方法末尾添加一个return
语句的原因。我会重构整个方法,不过…您需要做的就是在最后添加一个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();
}
公共静态长currentTimeMillis()
{
//把你的代码放在这里
对于(int y=0;yThanks,其目的是在currentTimeinMillis小于100时返回并打印currentTimeinMillis。您建议如何编写该方法?:)我对此仍然很陌生,非常感谢。