当前位置:编程学习 > Delphi >>

delphi错在什么地方了,求输入一个字符串中的英文、数字、其他字符的个数、求什么地方错了、需怎么该

var x,y,z:integer; a:string; begin a:='edit1.Text'; x:=0; if a in ['0'..'9'] then begin x:=x+1; end; y:=0; if (a in ['a'..'z']) or (a in ['A'..'Z']) then begin y:=y+1; end; z:=0; if a in ['0'..'9']) or (a in ['a'..'z']) or (a in ['A'..'Z']) then begin z:=i-1; end; edit2.text:=inttostr(x)+','+inttostr(y)+','+inttostr(z); end. 需要怎么改?
答案:按钮CLICK事件
var
  i,x,y,z:integer;
  a:string;
begin
  x:=0;
  y:=0;
  z:=0;
  a:=edit1.text;
  for i:=1 to length(a) do begin
    if (a[i] in  ['0'..'9']) then x:=x+1 ;
    if (a[i] in ['a'..'z']) or (a[i] in ['A'..'Z']) then y:=y+1;
    if not((a[i] in  ['0'..'9']) or (a[i] in ['a'..'z']) or (a[i] in ['A'..'Z'])) then z:=z+1;
  end;

  edit2.Text:=inttostr(x)+'  '+inttostr(y)+'   '+inttostr(z);
end;

在edit1中输入  Edit1中国
单击按钮后,edit2 中的结果是 1 4 4
其他:很明显地, a是字符串怎么与字符比较呢, 用a[ ] 来比较。下标从1开始的。 i:integer;

x:=0;
for i:=0 to lenth(a)-1 do begin 
  if a[i] in ['0'..'9'] then
  begin
   x:=x+1;
  end;
 //其他类似

end; a:='edit1.Text';===>a:=edit1.Text;
a in ['0'..'9'] ==>a[i] in ['0'..'9'] 
                       a[i] in ['a'..'z']) or (a[i] in ['A'..'Z']) 

上一个:跪求 delphi 达人帮忙解答几点疑问
下一个:delphi7中通过串口读电子秤显示的数,为什么读不出来?

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,