Java System.out.print()未打印到终端
这可能是一个非常愚蠢的问题,但我正试图修改Sedgewick的“算法”教科书中为一个类项目提供的代码。出于调试目的,我只想在不同的点向终端打印一些信息。例如,无论我在哪里插入System.out.println(“测试”),我都没有收到任何错误,程序运行到完成,但从未打印出任何内容。出于某种原因,打印的目标目的地是否可能不是终端Java System.out.print()未打印到终端,java,Java,这可能是一个非常愚蠢的问题,但我正试图修改Sedgewick的“算法”教科书中为一个类项目提供的代码。出于调试目的,我只想在不同的点向终端打印一些信息。例如,无论我在哪里插入System.out.println(“测试”),我都没有收到任何错误,程序运行到完成,但从未打印出任何内容。出于某种原因,打印的目标目的地是否可能不是终端 public class LZW { private static final int R = 256; // number of input c
public class LZW {
private static final int R = 256; // number of input chars
private static final int L = 4096; // number of codewords = 2^W
private static final int W = 12; // codeword width
public static void compress() {
String input = BinaryStdIn.readString();
TST<Integer> st = new TST<Integer>();
for (int i = 0; i < R; i++)
st.put("" + (char) i, i);
int code = R+1; // R is codeword for EOF
while (input.length() > 0) {
String s = st.longestPrefixOf(input); // Find max prefix match s.
BinaryStdOut.write(st.get(s), W); // Print s's encoding.
int t = s.length();
if (t < input.length() && code < L) // Add s to symbol table.
st.put(input.substring(0, t + 1), code++);
input = input.substring(t); // Scan past s in input.
}
BinaryStdOut.write(R, W);
BinaryStdOut.close();
}
public static void expand() {
String[] st = new String[L];
int i; // next available codeword value
// initialize symbol table with all 1-character strings
for (i = 0; i < R; i++)
st[i] = "" + (char) i;
st[i++] = ""; // (unused) lookahead for EOF
int codeword = BinaryStdIn.readInt(W);
if (codeword == R) return; // expanded message is empty string
String val = st[codeword];
while (true) {
BinaryStdOut.write(val);
codeword = BinaryStdIn.readInt(W);
if (codeword == R) break;
String s = st[codeword];
if (i == codeword) s = val + val.charAt(0); // special case hack
if (i < L) st[i++] = val + s.charAt(0);
val = s;
}
BinaryStdOut.close();
}
public static void main(String[] args) {
System.out.println("Testing123");
if (args[0].equals("-")) compress();
else if (args[0].equals("+")) expand();
else throw new IllegalArgumentException("Illegal command line argument");
}
公共类LZW{
私有静态final int R=256;//输入字符数
私有静态final int L=4096;//码字数=2^W
私有静态final int W=12;//码字宽度
公共静态void compress(){
字符串输入=BinaryStdIn.readString();
TST st=新的TST();
对于(int i=0;i0){
字符串s=st.longestprifixof(输入);//查找与s匹配的最大前缀。
BinaryStdOut.write(st.get(s),W);//打印s的编码。
int t=s.长度();
if(t
}也许你可以自己编写一个小测试程序,除了将“Hello World”写入standard out外,什么都不做,看看它是否作为基本情况打印。“打印的目标目标是否因为某种原因不是终端?”程序本身不太可能决定将标准输出重定向到某个地方。根据这里的代码:BinaryStdOut正在向standard out发送输出。我不认为我建议程序有意识地这样做,但我已经用System.out.print()测试了其他程序我假设
BinaryStdIn/Out
类使用标准流,因此您可以使用常规流重定向执行压缩/解压缩。在这种情况下,您不能写入标准输出,因为它会破坏压缩结果。也许可以尝试使用System.err.println()
来代替。也许可以编写一个小测试程序,除了将“Hello World”写入standard out之外,什么都不做,然后查看它是否作为基本情况打印。“打印的目标目标是否因为某种原因不是终端?”程序本身不太可能决定将标准输出重定向到某个地方。根据这里的代码:BinaryStdOut正在向standard out发送输出。我不认为我建议程序有意识地这样做,但我已经用System.out.print()测试了其他程序我假设BinaryStdIn/Out
类使用标准流,因此您可以使用常规流重定向执行压缩/解压缩。在这种情况下,您不能写入标准输出,因为它会破坏压缩结果。也许可以试试System.err.println()
。