我最近开始使用 Delphi 7 进行编码,主要用于学校,但也用于个人娱乐。我遇到了一个无法解决的问题。我想制作一个控制台应用程序,要求您输入几个字符串,例如您的姓名等,以便应用程序稍后可以通过您的名字来调用您。然而,我很快意识到有两个“read();”由于某种原因,两个不同字符串的命令不起作用,每次都会跳过第二个字符串的读取命令。由于我无法完美地解释它,所以这是我能想到的显示问题的最简单的代码:
program stringproblem;
{$APPTYPE CONSOLE}
uses
SysUtils;
var string1,string2:string;
begin
read(string1);
read(string2);
writeln(string1,string2);
readln;
readln;
end.
因此控制台打开,我开始写入 string1 的值,例如,我输入“test”,但它不会让我输入 string2 的值,而是跳过该值,只写出“test”在控制台中。
为什么我无法在应用程序中输入两个字符串的值?为什么写入第一个值会自动跳过所有其他值?
最佳答案
您应该使用Readln(string1)
,而不是Read(string1)
。对于其他阅读也是如此,事实上,每当您希望消耗整行时。
来自documentation对于读取
:
Read reads all characters up to, but not including, the next end-of-line marker or until Eof(F) becomes true; it does not skip to the next line after reading.
After the first Read, each subsequent Read sees the end-of-line marker and returns a zero-length string.
Use multiple Readln calls to read successive string values.
关于delphi - Delphi 7 编码新手 : Unable to read() multiple strings,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33158994/