-1

このケースの実装方法: 20 項目の CheckListBox があります: 症状 1、症状 2、..、症状 20。ユーザーは複数の症状を選択できます。私を混乱させるのは、それぞれの症状に複数の値を与える方法です。これが私のコードです:

  for i := 0 to CheckListBox1.Items.Count - 1 do
  begin
    if CheckListBox1.Checked[i] = True then
    begin
      Memo1.Lines.Append(CheckListBox1.Items.Strings[i]);
      if i = 0 
        p1 := 'Disease 1';
        p2 := 'Disease 2';
        p3 := 'Disease 3';
      if i = 1 then
        p1 := 'Disease 2';
      if i = 2 then
        p1 := 'Disease 1';
      if i = 3 then
        p1 := 'Disease 3';
      if i = 4 then
        p1 := 'Disease 2';
        p2 := 'Disease 3';
      if i = 5 then
        p1 := 'Disease 1';
        p2 := 'Disease 5';
        p3 := 'Disease 6';
      if i = 6 then
        p1 := 'Disease 5';

      Memo1.Lines.Add('Disease:' + p1+', '+p2+', '+p3);
      Memo1.Lines.Add('');
    end;
  end;
end;

しかし、結果は私が期待したものではありません。p1,p2,p3 を動的に作成するには?

インデックス 2,4,6 でチェックした結果は次のとおりです。

Symptomp 3
Disease:Disease 1, Disease 5, Disease 6

Symptomp 5
Disease:Disease 2, Disease 5, Disease 6

Symptomp 7
Disease:Disease 5, Disease 5, Disease 6
4

2 に答える 2

0

期待される結果が得られない考えられる理由の 1 つは、p1,p2およびp3変数をクリアしないことCheckListBox1.Checked[0]です。空欄。次のようなものを試してくださいp2p3CheckListBox1.Checked[1]p2p3

for i := 0 to CheckListBox1.Items.Count - 1 do
  begin
    if CheckListBox1.Checked[i] = True then
    begin
      p1 := '';
      p2 := '';
      p3 := '';

      Memo1.Lines.Append(CheckListBox1.Items.Strings[i]);

      if i = 0 then begin
        p1 := 'Disease 1';
        p2 := 'Disease 2';
        p3 := 'Disease 3';
      end;
      ...    
      Memo1.Lines.Add('Disease:' + p1+', '+p2+', '+p3);
      Memo1.Lines.Add('');
    end;
  end;
end;
于 2015-07-22T07:37:59.873 に答える
0

一致する疾患のリストと定数文字列配列を宣言します。

// List of diseases
type
  // Note: Use descriptive names instead of a numbers
  TDisease = (td1,td2,td3,{..,}tdMaxDiseases);
  TDiseaseSet = set of TDisease;

  TSymptom = (ts1,ts2,ts3,{..,}tsMaxSymptoms);

const
  // A list of disease names
  sDisease: array[TDisease] of String =
    ('Disease 1','Disease 2','Disease 3',{..,}'Disease xx');
  // An array of disease sets corresponding to each symptom
  cMyDiseaseSet : array[TSymptom] of TDiseaseSet = ([td1,td2,td3],[td3],[td1],[td2]);

セット定数配列は、各症状の病気のセットを宣言します。


各症状の結果の文字列と、症状に一致する病気のセットを取得するには:

// A Function to retrieve the diseases from a symptom
function DiseaseFromSymptom(aSymptom: TSymptom; var diseaseSet: TDiseaseSet): String;
var
  aDisease: TDisease;
begin
  diseaseSet := cMyDiseaseSet[aSymptom];
  Result := '';
  for aDisease in diseaseSet do
    Result := Result + sDisease[aDisease] + ', ';
  SetLength(Result,Length(Result)-2);
end;

var
  diseases,diseasesSummary: TDiseaseSet;
  s: String;

  diseasesSummary := [];
  for i := 0 to CheckListBox1.Items.Count - 1 do
  begin
    if CheckListBox1.Checked[i] = True then
    begin
      s := DiseaseFromSymptom(TSymptom(i),diseases);
      Memo1.Lines.Append(CheckListBox1.Items.Strings[i]);
      Memo1.Lines.Add('Disease:' + s);
      Memo1.Lines.Add('');
      // Insert diseases
      diseasesSummary := diseasesSummary + diseases;
    end;
  end;
  // A complete set of diseases in diseasesSummary

チェックしたすべての症状に一致する病気のセットが必要なようです。最新のアップデートでは、その方法が示されています。

于 2015-07-22T08:02:29.757 に答える