Java错误: cannot find symbol Rational()

标签 java compiler-errors acm-java-libraries

我只是按照我的老师的偏好使用 acm 包。
这个程序假设分配10000个有理对象,使它们成为垃圾,然后计算使用垃圾收集器之前和之后的空闲内存。然后,它应该打印垃圾收集器已清除的内存量。

import acm.program.*;

public class RuntimeGarbage extends ConsoleProgram {
    public void run() {
        println("Allocating 10000 Rational Objects");
        for(int i=1; i==10000; i++) {
            new Rational();
        }
        Runtime myRuntime = Runtime.getRuntime();
        long free_before = myRuntime.freeMemory();
    myRuntime.gc();
    long free_after = myRuntime.freeMemory();
    println("Garbage collection freed" + ((free_after)-(free_before)) + "bytes");
    }
}


问题在于,当我尝试编译代码时,cmd 显示以下内容:

:8: error: cannot find symbol 
new Rational(); 
with an arrow right below the R. 

问题是否出在大括号内对象的创建上?

最佳答案

编译器所说的是它不知道 Rational 类型是在哪里定义的。 是的,您可以在for循环的代码块中创建对象。

根据谷歌的说法,Rational 类型没有在 acm 包中定义

rational site:www-cs-faculty.stanford.edu/~eroberts/jtf/javadoc/student/acm/

所以它一定是在其他地方定义的。

它看起来也不属于内置的java类型 http://docs.oracle.com/javase/7/docs/api/

关于Java错误: cannot find symbol Rational(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20857819/

相关文章:

java - 上界通配符(扩展)不起作用;数组列表<? extends SuperType> 不允许子类型的实例

java - 制作 GImage 时出现 NullPointerException

java - 需要帮助在 Eclipse 中使用 ACM Java 库运行程序

java - 如何将相似的 Java 函数合并为一个(在 C++ 中我会使用模板)

java - 如何让 Spring Data Neo4j 和 Spring Data JPA 协同工作?

java - 了解 Android Visualizer 类

java - ACM 库简单示例不适用于 jdk 1.7.0_79

Java 数组 - 搜索数组中的元素,然后找到索引

android - 错误 :Execution failed for task ':app:transformClassesWithJarMergingForDebug'

ios - 在 Swift Core Data 中设置一对多关系的问题