Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Delphi 计算单词中的字符和子字符串_Delphi - Fatal编程技术网

Delphi 计算单词中的字符和子字符串

Delphi 计算单词中的字符和子字符串,delphi,Delphi,我必须阅读未知数量的单词,并在输入单词“last”时终止程序。 我必须: 显示以“S”开头的名称数 显示以“d”结尾的名称数 显示以“anne”结尾的姓名数 问题:我唯一的问题是显示以“anne”结尾的姓名的数量。它显示0,除非我只输入单词“anne”,否则它显示1 注: 我们还没有从数组开始 这是我在写测试前正在练习的作业,不应该交作业 我的代码: unit Ess_U; interface uses Windows, Messages, SysUtils, Variants,

我必须阅读未知数量的单词,并在输入单词“last”时终止程序。 我必须:

  • 显示以“S”开头的名称数
  • 显示以“d”结尾的名称数
  • 显示以“anne”结尾的姓名数
问题:我唯一的问题是显示以“anne”结尾的姓名的数量。它显示0,除非我只输入单词“anne”,否则它显示1

注:

  • 我们还没有从数组开始
  • 这是我在写测试前正在练习的作业,不应该交作业
我的代码:

unit Ess_U;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    redOut: TRichEdit;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
Var wrd : string;
    Scount, dcount, Namecount : integer;
begin
redOut.clear;
   Scount := 0;
   dcount := 0;
   Namecount := 0;


   repeat
    wrd := inputbox ('Input words', 'Enter any word', '');

    if (Pos('S',wrd) > 0) AND (wrd[1] = 'S') then
     begin
     inc(Scount);
     end

    else if (Pos('d',wrd) > 0) AND (wrd[length(wrd)] = 'd') then
     begin
     inc(dcount);
     end

    else if copy(wrd, length(wrd)-4,4) = 'anne' then
     begin
     inc(Namecount);
     end;

    until (wrd = 'last');

    redOut.Lines.Add ('The number of names that begin with the letter "S" is ' + inttostr(Scount));
    redOut.Lines.Add ('The number of names that end with the letter "d"  is ' + inttostr(dcount));
    redOut.Lines.Add ('The number of names that begin with "anne" is ' + inttostr(Namecount));
end;

end.

enter code here

您应该使用
StrUtils
中的
StartsText
EndsText
检查字符串是否以文本开头或结尾,而不是手动调用
Pos
Copy
。(或
AnsiStartsText
AnsiEndsText

StrUtils
添加到uses子句中,并使用以下条件更改该部分:

 ...
 uses StrUtils, ...;
 ...

 if (StartsText('S', wrd)) then
 begin
   Inc(Scount);
 end

 if (EndsText('d', wrd)) then
 begin
   Inc(dcount);
 end

 if (StartsText('anne', wrd)) then
 begin
   Inc(Namecount);
 end;
StrUtils
单元具有以下有用的例程:

  • -确定子字符串
    ASubText
    是否以字符串
    AText
    开头,不区分大小写比较
  • -与
    StartsText
    相同,但区分大小写
  • -确定子字符串
    ASubText
    是否以字符串
    AText
    结尾,不区分大小写比较
  • -与
    EndsStr
    相同,但区分大小写

您已经有了一个答案,它向您展示了另一种方法,即使用库函数来完成您正在尝试的操作。这很好(假设您的Delphi版本有它们),但您的问题还有一个未解决的方面

你说“问题:我唯一的问题是显示以“anne”结尾的姓名的数量。它显示0,除非我只输入“anne”一词,否则它显示1。”

现在,编码的一个关键部分是学习调试,其中的一个关键部分是准确的观察并知道如何构建一个可复制的测试用例

试试这个:

repeat
循环的第一行更改为:

wrd := 'xanne'; //inputbox ('Input words', 'Enter any word', '');
将“anne”的测试改为

else begin
  wrd := copy(wrd, length(wrd)-4,4);
  Caption := wrd;
  if wrd = 'anne' then
    begin
     inc(Namecount);
    end;
end;
然后,在该行上放置一个断点

  wrd := copy(wrd, length(wrd)-4,4);
然后按
F9
编译并运行程序

当调试器在断点处停止时,继续按
F8
(单步执行代码)

你很快就会明白问题出在哪里,也就是说

 copy(wrd, length(wrd)-4,4)
当wrd以“xanne”开头时,它不等于“anne”。我会让你去弄清楚为什么不等于“anne”。因为我认为你会发现,这比仅仅了解一个新的库函数更有启发性


顺便说一句,当你试图通过反复输入输入来测试一个程序时,这种情况往往会发生。这就是为什么我说要临时修改你的代码,这样你就可以从一个已知的输入开始,而不是一个你可能输入错误的输入(或者甚至错误地打开了CapsLock而没有注意到).

在编程中,必须注意细节,其程度几乎可以称为挑剔。因此,关于任务的定义,这里是我的“挑剔”。你说用户输入单词,然后你应该计算具有某些特殊功能的名称。好吧,我可以接受名称是单词,但所有单词都不是名称。例如,“对不起”是一个名称吗?在我的书中没有,但您似乎将其作为一个名称来计算。要按照您的定义执行,您需要一个所有现有名称的列表,您可以从中查找输入的单词,以验证它是否是一个名称。您实现中的一个特点是输入名称“Susanne”。它以“s”开头,因此您可以使用c根据第一条规则计算,但它是否也应该根据第三条规则计算?因为你有一个
if-then-else-if-then-else…
Susanne不算是一个“…anne”的名字。我希望Susanne不会为此感到难过。最后一个挑剔是最后一次
重做.line。添加(…
line。它说*…从开始“anne”…当你的任务定义是“…以“anne”结尾时,+1表示你的尝试和对问题动机的开放。