我需要复习一下将类从一个文件移动到两个文件。我的示例代码在一个名为“ external_class_file_main ”的文件中。程序运行良好,代码如下所示:
Public class external_class_file_main {
public static int get_a_random_number (int min, int max) {
int n;
n = (int)(Math.random() * (max - min +1)) + min;
return (n);
}
public static void main(String[] args) {
int r;
System.out.println("Program starting...");
r = get_a_random_number (1, 5);
System.out.println("random number = " + r);
System.out.println("Program ending...");
}
}
我将get_a_random_number类移动到一个名为“ external_class_file ”的单独文件中。当我这样做时,我收到以下错误:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method get_a_random_number(int, int) is undefined for the type
external_class_file_main
at external_class_file_main.main(external_class_file_main.java:20)
“ external_class_file_main ” 现在包含:
public class external_class_file_main {
public static void main(String[] args) {
int r;
System.out.println("Program starting...");
r = get_a_random_number (1, 5);
System.out.println("random number = " + r);
System.out.println("Program ending...");
}
}
“ external_class_file ”现在包含:
public class external_class_file {
public static int get_a_random_number (int min, int max) {
int n;
n = (int)(Math.random() * (max - min +1)) + min;
return (n);
}
}