我正在尝试编写一个简单的程序,它接受一个非素数并返回它的第一个因子.我必须使用一种方法来做到这一点.我认为我非常接近正确的代码,但我仍然在我的方法中遇到变量定义问题.这是我的(目前不正确)代码:
public class testing {
public static void main(String[] args) {
int a;
a = 42;
System.out.println(factor(a));
}
//This method finds a factor of the non-prime number
public static int factor(int m) {
for(int y=2 ; y <= m/2 ; y++) {
if(m%y==0) {
return y;
continue;
}
}
return y;
}
}
Run Code Online (Sandbox Code Playgroud)
请让我知道什么是不正确的!