公因數程式設計問題 |
答題得分者是:P.D.
|
l3161316
一般會員 發表:4 回覆:0 積分:0 註冊:2009-09-26 發送簡訊給我 |
這是我寫的
[code delphi] procedure TForm1.Button1Click(Sender: TObject); var a,b,c,i : integer; begin For i := 1 to StrToInt(Edit1.Text) do begin If StrToInt(Edit1.text) mod i = 0 then a := i; For c := 1 to StrToInt(Edit2.Text) do If (StrToInt(Edit2.Text) mod c = 0) and (a = c) then b := c; Label3.Caption := Label3.Caption IntToStr(b) ; end; end; [/code] 執行之後 結果都會變成 1 222 55555 10101010101010 這個樣子 可以說一下我哪邊寫錯或者沒寫到嗎? ------------------------------------------------------ 雖然用你的程式碼可以執行 但是我不太清楚哪邊錯了耶 為什麼把每個if for都加入begin end; 這樣執行就OK了? 如果是這一段 If (StrToInt(Edit2.Text) mod c = 0) and (a = c) then b := c; Label3.Caption := Label3.Caption IntToStr(b) ; 忘記加begin end;我還可以了解是這邊出錯 可以跟我說我哪邊搞錯了嗎QQ? ----------------------------------------------------------------- 喔喔~~我知道差在哪邊了 If StrToInt(Edit1.text) mod i = 0 then a := i; 這一段begin end;的地方錯誤 所以數字才會一直重複!! 3Q!!
------
0.0 |
P.D.
版主 發表:603 回覆:4038 積分:3874 註冊:2006-10-31 發送簡訊給我 |
給你參考
[code delphi] procedure TForm1.Button1Click(Sender: TObject); var a,b,c,i : integer; e1, e2: integer; begin Label3.Caption:= ''; e1:= StrToInt(Edit1.Text); e2:= StrToInt(Edit2.Text); For i := 1 to e1 do begin If e1 mod i = 0 then begin a := i; For c := 1 to e2 do begin If (e2 mod c = 0) and (a = c) then begin b := c; Label3.Caption := Label3.Caption ' ' IntToStr(b) ; break; end; end; end; end; end; [/code] |
eaglewolf
資深會員 發表:4 回覆:268 積分:429 註冊:2006-07-06 發送簡訊給我 |
你的寫法 如果用begin end 包起來會是
[code delphi] For i := 1 to StrToInt(Edit1.Text) do begin If StrToInt(Edit1.text) mod i = 0 then begin a := i; end; For c := 1 to StrToInt(Edit2.Text) do begin If (StrToInt(Edit2.Text) mod c = 0) and (a = c) then begin b := c; end; end; Label3.Caption := Label3.Caption IntToStr(b) ; end; [/code] 看出來差異在哪嗎??? ===================引 用 l3161316 文 章=================== 這是我寫的 [code delphi] procedure TForm1.Button1Click(Sender: TObject); var a,b,c,i : integer; begin For i := 1 to StrToInt(Edit1.Text) do begin If StrToInt(Edit1.text) mod i = 0 then a := i; For c := 1 to StrToInt(Edit2.Text) do If (StrToInt(Edit2.Text) mod c = 0) and (a = c) then b := c; Label3.Caption := Label3.Caption IntToStr(b) ; end; end; [/code] 執行之後 結果都會變成 1 222 55555 10101010101010 這個樣子 可以說一下我哪邊寫錯或者沒寫到嗎? ------------------------------------------------------ 雖然用你的程式碼可以執行 但是我不太清楚哪邊錯了耶 為什麼把每個if for都加入begin end; 這樣執行就OK了? 如果是這一段 If (StrToInt(Edit2.Text) mod c = 0) and (a = c) then b := c; Label3.Caption := Label3.Caption IntToStr(b) ; 忘記加begin end;我還可以了解是這邊出錯 可以跟我說我哪邊搞錯了嗎QQ?
------
先查HELP 再查GOOGLE 最後才發問 沒人有義務替你解答問題 在標題或文章中標明很急 並不會增加網友回答速度 Developing Tool: 1.Delphi 6 2.Visual Studio 2005 3.Visual Studio 2008 DBMS: MS-SQL |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |