gpt4 book ai didi

linux - 关于 Pascal 输出的一些茫然

转载 作者:太空宇宙 更新时间:2023-11-04 04:27:16 24 4
gpt4 key购买 nike

平台:Linux 2.6.18 x86_64

编译器:适用于 x86_64 的 Free Pascal 编译器版本 2.2.2 [2008/11/05]

源代码为:

Program main;
var invalue:string;
Begin
(*Until the EOF, this loop continue to work*)
while not eof do
begin
Write('Please input the express: ');
Flush(StdOut);
Readln(invalue);
Writeln('The expression is: ',invalue);
end;
Writeln('');
Writeln('Exit the program. Bye!');
End.

我编译并运行了它。但输出是这样的:

123
Please input the express: The expression is: 123
234
Please input the express: The expression is: 234
345
Please input the express: The expression is: 345

Exit the program. Bye!

这些数字是我输入的。我用谷歌搜索了一下,认为这是因为缓冲区的原因。所以我尝试在写入之后添加刷新(stdout)或刷新(输出)。但效果还是不太好。

我希望它的工作原理是:

Please input the express: 123
The expression is: 123
Please input the express: 234
The expression is: 234
Please input the express: 345
The expression is: 345

Exit the program. Bye!

谢谢!对不起我的傻瓜~

<小时/>

补充:我尝试过(谢谢你,Aloush!)

Program main;
var invalue:string;
Begin
(*Until the EOF, this loop continue to work*)
while not eof do
begin
Write('Please input the express: ');
Flush(StdOut);
Readln(invalue);
Writeln('')
Writeln('The expression is: ',invalue);
end;
Writeln('');
Writeln('Exit the program. Bye!');
End.

结果仍然不太好:

1
Please input the express:
The expression is: 1
2
Please input the express:
The expression is: 2
3
Please input the express:
The expression is: 3
4
Please input the express:
The expression is: 4
5
Please input the express:
The expression is: 5

Exit the program. Bye!

Readln 仍然在第一次 Write 之前执行。

顺便说一句,我也尝试过:

Program main;
var invalue:string;
Begin
(*Until the EOF, this loop continue to work
while not eof do
begin*)
Repeat
Write('Please input the express: ');
Flush(StdOut);
Readln(invalue);
Writeln('The expression is: ',invalue);
Until eof;
Writeln('');
Writeln('Exit the program. Bye!');
End.

在这种情况下,第一个循环是好的,但其他循环仍然是错误的。

Please input the express: 123
The expression is: 123
234
Please input the express: The expression is: 234
345
Please input the express: The expression is: 345

Exit the program. Bye!

谢谢!

<小时/>

最终解决方案: http://www.amath.unc.edu/sysadmin/DOC4.0/pascal/lang_ref/ref_io.doc.html#592这是因为,eof实际上对应的是隐式读取。当前的代码应该是:

Program main;
var invalue:string;
Begin
(*Until the EOF, this loop continue to work*)
Write('Please input the express: ');
while not Eof do
begin
Readln(invalue);
Writeln('The expression is: ',invalue);
Write('Please input the express: ');
end;
Writeln('');
Writeln('Exit the program. Bye!');
End.

最后,谢谢这里的Aloush和CSDN上的kei!

最佳答案

你能尝试以下方法吗?

Program main;
var invalue:string;
Begin
(*Until the EOF, this loop continue to work*)
while not eof do
begin
Write('Please input the express: ');
Flush(StdOut);
Readln(invalue);
Writeln('')
Writeln('The expression is: ',invalue);
end;
Writeln('');
Writeln('Exit the program. Bye!');
End.

关于linux - 关于 Pascal 输出的一些茫然,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7749331/

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