2016-02-21 106 views
0

我必須要求用戶輸入單詞(一次一個),直到輸入「xxx」。然後我必須用這些詞在每個詞之間留出一個空格來構建一個句子。只要輸入終止字符串(「xxx」),就必須顯示該句子。如何將單詞轉換爲句子?

我的代碼只顯示「xxx」,終止代碼。

注意:我們尚未開始使用陣列。

這是我試過到目前爲止代碼:

unit BuildSentence_U; 

interface 

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

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

var 
    Form1: TForm1; 

implementation 

{$R *.dfm} 

procedure TForm1.btnResultClick(Sender: TObject); 
Var wrd : string; 
begin 

wrd := inputbox('Word input', ' Enter any word to build a sentence  (press "xxx" to stop)',''); 
redOut.Clear; 

while (wrd <> 'xxx') do 
begin 
    wrd := wrd + ' '; 
    wrd := inputbox('Word input', ' Enter any word to build a sentence (press"xxx" to stop)',''); 
end; 
redOut.Lines.Add(wrd); 
end; 

end. 

回答

1

使用另一個字符串來收集詞放在一起:

Var 
    wrd, sentence : string; 
begin 

wrd := inputbox('Word input', ' Enter any word to build a sentence  (press "xxx" to stop)',''); 
redOut.Clear; 

while (wrd <> 'xxx') do 
begin 
    sentence := sentence + wrd + ' '; 
    wrd := inputbox('Word input', ' Enter any word to build a sentence (press"xxx" to stop)',''); 
end; 
redOut.Lines.Add(sentence);