Delphi 7 中的编码新手:无法读取 () 多个字符串
New to coding in Delphi 7: Unable to read() multiple strings
我最近在Delphi 7 开始编码,主要用于学校,但也用于个人娱乐。我有 运行 个我无法解决的问题。我想制作一个控制台应用程序,要求您输入几个字符串,例如您的姓名和类似的字符串,以便该应用程序稍后可以通过您的名字来呼叫您。但是,我很快意识到,出于某种原因,对 2 个不同的字符串使用两个 "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 for Read
:
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 7 开始编码,主要用于学校,但也用于个人娱乐。我有 运行 个我无法解决的问题。我想制作一个控制台应用程序,要求您输入几个字符串,例如您的姓名和类似的字符串,以便该应用程序稍后可以通过您的名字来呼叫您。但是,我很快意识到,出于某种原因,对 2 个不同的字符串使用两个 "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 for Read
:
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.