-1

我经常在使用 NetBeans 时遇到问题。早些时候我能够运行这个程序没问题,但现在,它在运行后告诉我这个

“线程“主”java.lang.RuntimeException 中的异常:无法编译的源代码 - 找不到符号符号:类 Loop1 位置:hw7.main(hw7.java:72) 处的类 hw7”

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();
}

}

4

2 回答 2

2

您没有正确实例化该类。由于它的名称是forLoops,您需要执行以下操作:

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

与问题无关,请注意 Java 中的类按照惯例以大写字母开头

于 2013-10-19T00:17:07.527 回答
2

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

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

于 2013-10-19T00:22:32.740 回答