Linux 有些人对帕斯卡的输出感到迷惑不解
平台:Linux 2.6.18 x86_64 编译器:用于x86_64的免费Pascal编译器版本2.2.2[2008/11/05] 源代码是:Linux 有些人对帕斯卡的输出感到迷惑不解,linux,pascal,Linux,Pascal,平台:Linux 2.6.18 x86_64 编译器:用于x86_64的免费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 expr
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!
这些数字是我的输入。我在谷歌上搜索了一下,认为这是因为缓冲区。因此,我尝试在写入之后添加flushstdout或flushoutput。但它仍然不能很好地工作
我希望它的作用如下:
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!
谢谢大家!!为我的傻瓜感到抱歉~
补充:
我试着谢谢你,阿卢什
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仍在第一次写入之前执行
顺便说一下,我还尝试:
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!
谢谢大家!
最终解决方案:
这是因为,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.
最后,谢谢你,阿卢什和凯伊在CSDN 你能试试下面的方法吗
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.
请勾选答案左边的箭头,将其标记为已接受。这比编辑要解决的标题要好。