gpt4 book ai didi

Java 推回输入流

转载 作者:行者123 更新时间:2023-12-01 12:17:30 26 4
gpt4 key购买 nike

为什么它打印“.eq.”对于单个 = 符号,而它实际上应该打印在 == 符号上。请参阅下面的评论。 f.read() 与此有什么关系吗?

import java.io.*;
public class FPushBackInputStreamDemo {
public static void main(String[] args) throws IOException {
String s = "if (a == 4) a = 0;\n";
byte b[] = s.getBytes();
ByteArrayInputStream b1 = new ByteArrayInputStream(b);
PushbackInputStream p = new PushbackInputStream(b1);
int c;

while((c=p.read()) != -1)
{
switch(c)
{
case '=':
if((c = p.read()) == '=') //here it is checking for single '=' sign then why its printing ".eq." for both '==' signs?
{
System.out.print(".eq.");
}
else
{
System.out.print("<-");
p.unread(c);

}
break;
default:
System.out.print((char) c);
break;
}
}
}

}

输出: enter image description here

最佳答案

它会打印 .eq. 作为双等号。

在该 if 语句处,您处于已经读取了前面的另一个 =case 中。

while((c=p.read()) != -1)        // read one character
{
switch(c) // see what it is
{
case '=': // that was a "="
if((c = p.read()) == '=') // read ANOTHER character

请注意,在 else 部分,您必须推回第二个字符,因为它实际上尚未被处理(您刚刚确认它不是第二个 =).

  p.unread(c);

关于Java 推回输入流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26901308/

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