我正在使用Java处理一些简单的数据结构,并且正在使用Princeton的库来实现数据结构,但是VS Code无法选择import语句下使用的文件,但是如果我编译并运行终端上的程序。
这是我的Java代码,上面有描述情况的注释:
//these imports work fine
import java.util.Iterator;
import java.util.NoSuchElementException;
//this is available in my local directory
//VS code is unable to resolve these imports, however it works fine while using integrated terminal
import edu.princeton.cs.algs4.Bag;
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;
public class Stats {
public static void main(String[] args) {
// read in numbers
Bag<Double> numbers = new Bag<Double>();
int i = 0;
while (i < args.length) {
numbers.add(Double.parseDouble(args[i]));
i++;
}
int n = numbers.size();
// compute sample mean
double sum = 0.0;
for (double x : numbers)
sum += x;
double mean = sum / n;
// compute sample standard deviation
sum = 0.0;
for (double x : numbers) {
sum += (x - mean) * (x - mean);
}
double stddev = Math.sqrt(sum / (n - 1));
StdOut.printf("Mean: %.2f\n", mean);
StdOut.printf("Std dev: %.2f\n", stddev);
}
}
这是我在VS Code构建错误中收到的内容:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
StdOut cannot be resolved
StdOut cannot be resolved
有人可以帮我吗?我正在使用Microsoft的Java Extension Pack
答案 0 :(得分:1)
任何应该编译或运行Java代码的IDE都需要具有所需的类。换句话说:
答案 1 :(得分:0)
要添加到GhostCat的答案中(我正在普林斯顿大学(Princeton)做相同的Coursera课程),此信息很有帮助:
https://code.visualstudio.com/docs/java/java-project
Tl; dr:我必须将此添加到vscode项目目录中的.classpath
文件中:
<classpathentry kind="lib" path="/path_to_stdlib.jar" />