我经常遇到NetBeans问题。之前我能够运行这个程序没问题,但现在,它在运行后告诉我这个
“线程中的异常”主“java.lang.RuntimeException:无法编译的源代码 - 无法找到符号 符号:类Loop1 位置:类hw7 在hw7.main(hw7.java:72)“
import java.util.Scanner;
class forLoops {
void forLoop1(){
Scanner in = new Scanner(System.in);
int cnt = 2;
System.out.print("Enter n:");
int n = in.nextInt();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++){
if (i == 1)
System.out.print(1);
else if (i > 1) {
System.out.printf("%3d", cnt);
cnt++;
}
}
System.out.println();
}
}
void forLoop2(){
Scanner in = new Scanner(System.in);
System.out.print("Enter n:");
int n = in.nextInt();
for (int i = 1; i < n+1; i++) {
int sum = 0;
for (int j = 0; j < i; j++){
System.out.printf("%3d", i+sum);
sum = sum + n-(j+1);
}
System.out.println();
}
}
void forLoop3(){
Scanner in = new Scanner(System.in);
System.out.print("Enter n:");
int n = in.nextInt();
int x = 1;
for (int i = 1; i < n+1; i++) {
int sum = 0;
for (int j = 0; j < i; j++){
System.out.printf("%3d", i+sum);
sum = sum + n-(j+1);
}
System.out.println();
}
}
}
public class hw7 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
forLoops myL1 = new forLoops();
myL1.forLoop1();
forLoops myL2 = new forLoops();
myL2.forLoop2();
Loops myL3 = new forLoops();
myL3.forLoop3();
}
}
答案 0 :(得分:2)
您没有正确地实例化该类。由于它的名称是forLoops
,因此您需要执行以下操作:
forLoops myL1 = new forLoops();
myL1.forLoop1();
forLoops myL2 = new forLoops();
myL2.forLoop2();
forLoops myL3 = new forLoops();
myL3.forLoop3();
与问题无关,请注意,按照惯例,Java 中的类以大写字母开头。
答案 1 :(得分:2)
这是第72行:Loop1 myL1 = new Loop1();
您正在使用名为Loop1的类,但尚未定义它。