2016-03-06 33 views
0

我正在学习Java,我正在使用BlueJ编写返回currentTimeInMillis()的方法,而y < 100.此刻,我收到一个错误声明“缺少回报声明”。任何建议重新错误/代码?Java - for循环currentTimeInMillis() - BlueJ

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

所有你需要做的是在结尾处添加一个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(); 

} 
+0

:)我仍然很新,在此,非常感谢。 – user3289631

0

您的currentTimeMillis应该返回一个长。这个签名

public static long currentTimeMillis() 

状态,你私人静态方法,必须返回类型的值。

编码人员可以假定for循环将始终执行,但编译器不能,这就是为什么您必须在该方法结尾处添加return语句的原因。我虽然重构整个方法...

+0

谢谢,目的是为了恢复和打印currentTimeinMillis虽然它不太你会怎么建议写这个方法? – user3289631