gpt4 book ai didi

java - 编译错误 : cannot find symbol: In, StdIn 和 StdOut

转载 作者:太空狗 更新时间:2023-10-29 22:59:38 42 4
gpt4 key购买 nike

代码来自 http://algs4.cs.princeton.edu/11model/BinarySearch.java.html用于算法教科书。

import java.util.Arrays;

public class BinarySearch {

// precondition: array a[] is sorted
public static int rank(int key, int[] a) {
int lo = 0;
int hi = a.length - 1;
while (lo <= hi) {
// Key is in a[lo..hi] or not present.
int mid = lo + (hi - lo) / 2;
if (key < a[mid]) hi = mid - 1;
else if (key > a[mid]) lo = mid + 1;
else return mid;
}
return -1;
}

public static void main(String[] args) {
int[] whitelist = In.readInts(args[0]);

Arrays.sort(whitelist);

// read key; print if not in whitelist
while (!StdIn.isEmpty()) {
int key = StdIn.readInt();
if (rank(key, whitelist) == -1)
StdOut.println(key);
}
}
}

我收到这个错误

$ javac BinarySearch.java 
BinarySearch.java:44: cannot find symbol
symbol : variable In
location: class BinarySearch
int[] whitelist = In.readInts(args[0]);
^
BinarySearch.java:49: cannot find symbol
symbol : variable StdIn
location: class BinarySearch
while (!StdIn.isEmpty()) {
^
BinarySearch.java:50: cannot find symbol
symbol : variable StdIn
location: class BinarySearch
int key = StdIn.readInt();
^
BinarySearch.java:52: cannot find symbol
symbol : variable StdOut
location: class BinarySearch
StdOut.println(key);
^
4 errors

最佳答案

StdInStdOutIn 不是标准 Java 库的一部分。它们是与普林斯顿类(class)一起提供的支持类(class)。

来自1.1 Programming Model源代码中链接的页面:

Standard input and standard output. StdIn.java and StdOut.java are libraries for reading in numbers and text from standard input and printing out numbers and text to standard output. Our versions have a simpler interface than the corresponding Java ones (and provide a few tecnical improvements).

...

In.java and Out.java are object-oriented versions that support multiple input and output streams, including reading from a file or URL and writing to a file.

因此,如果您想按原样使用二进制搜索代码,则需要下载这些文件。

关于java - 编译错误 : cannot find symbol: In, StdIn 和 StdOut,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16207427/

42 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com