首页 文章

为什么我的编译器告诉我有一个Uncompilable源代码?

提问于
浏览 1369
-1

我经常遇到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();
}

}

2 回答

  • 2

    你're not instantiating the class correctly. Since it'的名字是 forLoops ,你需要做类似的事情:

    forLoops myL1 = new forLoops();
    myL1.forLoop1();
    forLoops myL2 = new forLoops();
    myL2.forLoop2();
    forLoops myL3 = new forLoops();
    myL3.forLoop3();
    

    与问题无关,请按照惯例注意Java start with a capital letter 中的类 .

  • 2

    这是第72行: Loop1 myL1 = new Loop1();

    您正在使用名为Loop1的类,但尚未定义它 .

相关问题