function 的問題 |
|
majan
一般會員 發表:10 回覆:10 積分:4 註冊:2007-02-20 發送簡訊給我 |
程式碼f
function drawCld(SY : Integer): String; var SY : integer; yDisplay : string; begin if ( year1 >= 1875 & year1 =< 1908 ) then begin yDisplay := '光緒' IntToStr(year1 - 1874)); end; if ( year1 >= 1909 & year1 <= 1911 ) then begin yDisplay := '宣統' IntToStr(year1 - 1908); end; if ( year1>=1912 ) then begin yDisplay := '民國' IntToStr(year1 - 1911); end; Result:= yDisplay; end; 問題是 我要用整數去取回傳回去要用 string 這樣寫對嗎? 其實我想問的是 這個if 判斷是那裡錯 if (year1 > 1 and year1 < 2500 ) then begin Edit1.Text:='111'; end |
Stallion
版主 發表:52 回覆:1600 積分:1995 註冊:2004-09-15 發送簡訊給我 |
你的函數本身就有錯誤吧!
參數SY 在函數的形式上參數與函數內的區域參數分別都宣告了,SY他到底是什麼?還有year1是從哪裡跑出來的?難道是全域變數嗎? ===================引 用 文 章=================== 程式碼f function drawCld(SY : Integer): String; var SY : integer; yDisplay : string; begin if ( year1 >= 1875 & year1 =< 1908 ) then begin yDisplay := '光緒' IntToStr(year1 - 1874)); end; if ( year1 >= 1909 & year1 <= 1911 ) then begin yDisplay := '宣統' IntToStr(year1 - 1908); end; if ( year1>=1912 ) then begin yDisplay := '民國' IntToStr(year1 - 1911); end; Result:= yDisplay; end; 問題是 我要用整數去取回傳回去要用 string 這樣寫對嗎? 其實我想問的是 這個if 判斷是那裡錯 if (year1 > 1 and year1 < 2500 ) then begin Edit1.Text:='111'; end |
majan
一般會員 發表:10 回覆:10 積分:4 註冊:2007-02-20 發送簡訊給我 |
基本上if 的問題我己知道的
function 也沒問題 procedure TForm3.BitBtn1Click(Sender: TObject); var DT:TDatetime; ST:TSystemtime; S:string; SY : integer; //yDisplay : string; begin //Edit1.SetText:=DateTimeToStr(system.DateTime); DT :=Date; //S:=DateToStr(DT); year1 := YearOf(DT); month1 := MonthOf(DT); day1 := DayOf(DT); Edit1.Text := drawCld(year1); ........... 這段為什麼會有問題 ? end; function drawCld(year2 : Integer): String; //year2 : integer; var yDisplay : string; begin if (( year2 >=1875) and (year2 <=1908 )) then begin yDisplay := '光緒' IntToStr(year2 - 1874); end; if (( year2 >= 1909) and (year2 <= 1911 )) then begin yDisplay := '宣統' IntToStr(year2 - 1908); end; if ( year2>=1912 ) then begin yDisplay := '民國' IntToStr(year2 - 1911); end; // Edit1.Text:=yDisplay; Result:= yDisplay; end; |
Stallion
版主 發表:52 回覆:1600 積分:1995 註冊:2004-09-15 發送簡訊給我 |
首先不論你的 year1, month1, day1 哪裡來的,單純就函數呼叫可能造成的錯誤,應該是視野(Scope)造成的,解決這個問題兩種方案。
1.將下面這兩個函數位置顛倒過來。 2.做前置宣告如下: function drawCld(year2 : Integer): String;forward; procedure TForm3.BitBtn1Click(Sender: TObject); var DT:TDatetime; ST:TSystemtime; S:string; SY : integer; //yDisplay : string; begin //Edit1.SetText:=DateTimeToStr(system.DateTime); DT :=Date; //S:=DateToStr(DT); year1 := YearOf(DT); month1 := MonthOf(DT); day1 := DayOf(DT); Edit1.Text := drawCld(year1); ........... 這段為什麼會有問題 ? end; function drawCld(year2 : Integer): String; //year2 : integer; var yDisplay : string; begin if (( year2 >=1875) and (year2 <=1908 )) then begin yDisplay := '光緒' IntToStr(year2 - 1874); end; if (( year2 >= 1909) and (year2 <= 1911 )) then begin yDisplay := '宣統' IntToStr(year2 - 1908); end; if ( year2>=1912 ) then begin yDisplay := '民國' IntToStr(year2 - 1911); end; // Edit1.Text:=yDisplay; Result:= yDisplay; end; |
majan
一般會員 發表:10 回覆:10 積分:4 註冊:2007-02-20 發送簡訊給我 |
unit Unit3;
interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, System.ComponentModel, Borland.Vcl.StdCtrls, Borland.Vcl.Buttons, DateUtils; type TForm3 = class(TForm) BitBtn1: TBitBtn; Edit1: TEdit; procedure BitBtn1Click(Sender: TObject); // function drawCld(year2 : Integer): String;forward; private { Private declarations } public var lunarInfo : Array[1..201] of string; conWeekend : integer; solarMonth : Array[1..12] of integer; Gan : Array[1..10] of string; Zhi : Array[1..12] of string; solarTerm : Array[1..24] of string; sTermInfo : Array[1..24] of integer; nStr1 : Array[1..11] of string; nStr2 : Array[1..5] of string; monthName : Array[1..12] of string; year1,month1,day1: integer; { Public declarations } end; var Form3: TForm3; implementation {$R *.nfm} procedure TForm3.BitBtn1Click(Sender: TObject); var DT:TDatetime; ST:TSystemtime; S:string; SY : integer; //yDisplay : string; begin //Edit1.SetText:=DateTimeToStr(system.DateTime); DT :=Date; //S:=DateToStr(DT); year1 := YearOf(DT); month1 := MonthOf(DT); day1 := DayOf(DT); Edit1.Text := drawCld(year1); end; function drawCld(year2 : Integer): String; //year2 : integer; var yDisplay : string; begin if (( year2 >=1875) and (year2 <=1908 )) then begin yDisplay := '光緒' IntToStr(year2 - 1874); end; if (( year2 >= 1909) and (year2 <= 1911 )) then begin yDisplay := '宣統' IntToStr(year2 - 1908); end; if ( year2>=1912 ) then begin yDisplay := '民國' IntToStr(year2 - 1911); end; // Edit1.Text:=yDisplay; Result:= yDisplay; end; function cyclical(num:Integer):Integer; const Gan : Array[1..10] of string = ('甲','乙','丙','丁','戊','己','庚','辛','壬','癸'); Zhi : Array[1..12] of string = ('子','丑','寅','卯','辰','巳','午','未','申','酉','戌','亥'); begin // return(Gan[num] Zhi[num]); end; end. 對delphi 的 我不太曉得怎麼用副程式 我試過大大教的 但還是 有問題 |
Stallion
版主 發表:52 回覆:1600 積分:1995 註冊:2004-09-15 發送簡訊給我 |
找個空閒把你的範例改了一下,請參考!
<textarea cols="60" rows="10" class="delphi" name="code"> unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs,DateUtils, StdCtrls; type TForm1 = class(TForm) Button1: TButton; Edit1: TEdit; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} //你的原程式寫成C++的語法,不要搞混了! function Cyclical(num:Integer):String; //這個函數在這之程式沒有用上 const Gan:Array[1..10] of string = ('甲','乙','丙','丁','戊','己','庚','辛','壬','癸'); Zhi:Array[1..12] of string = ('子','丑','寅','卯','辰','巳','午','未','申','酉','戌','亥'); begin Result := Gan[num mod 10] Zhi[num mod 12]; end; function drawCld(year2 : Integer): String; begin if (( year2 >=1875) and (year2 <=1908 )) then Result := '光緒' IntToStr(year2 - 1874) else if (( year2 >= 1909) and (year2 <= 1911 )) then Result := '宣統' IntToStr(year2 - 1908) else if ( year2>=1912 ) then Result := '民國' IntToStr(year2 - 1911); end; procedure TForm1.Button1Click(Sender: TObject); var DT:TDatetime; year1,month1,day1:Word; begin DT := Date; year1 := YearOf(DT); month1 := MonthOf(DT); day1 := DayOf(DT); Edit1.Text := drawCld(year1); end; end. end. </textarea> |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |