可能我在检查Java核心时遗漏了一些东西,但是请帮助我理解为什么我不能使用在java main方法中声明的方法进行评论
long double rand_01_ld(void) {
// These should be calculated once rather than each function call
// Leave that as a separate implementation problem
// Assume RAND_MAX is power-of-2 - 1
assert((RAND_MAX & (RAND_MAX + 1U)) == 0);
double rand_max_p1 = (RAND_MAX/2 + 1)*2.0;
unsigned BitsPerRand = (unsigned) round(log2(rand_max_p1));
assert(FLT_RADIX != 10);
unsigned BitsPerFP = (unsigned) round(log2(FLT_RADIX)*LDBL_MANT_DIG);
long double r = 0.0;
unsigned i;
for (i = BitsPerFP; i >= BitsPerRand; i -= BitsPerRand) {
r += rand();
r /= rand_max_p1;
}
if (i) {
r += rand() % (1 << i);
r /= 1 << i;
}
return r;
}
答案 0 :(得分:1)
您无法在其他方法中声明方法。
public static int cal2 ( int a, int b){
return a + b;
}
public static void main(String arg[]) {
int ab2 = cal2(2);
System.out.println(ab2);
R r = new R();
int ab = r.cal(2, 2);
System.out.println(ab);
int ab3 =r.cal3(2,3);
System.out.println(ab3);
}
答案 1 :(得分:1)
正如其他人所说,你不可能在另一个内部定义一个典型的方法。但是,为了清楚起见,使用BiFunction
接口有一个等价物(Java8 - ,如果仍需要陈述)。例如,
public static void main(String[] args) {
BiFunction<Integer, Integer, Integer> func = (a, b) -> a + b;
System.out.println(func.apply(3, 4));
}