如何实施此案例: 我有两个项目的CheckListBox:Symptomp 1,Symptomp 2,..,Symptomp 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
答案 0 :(得分:0)
您未获得预期结果的一个可能原因是您从未清除p1
,p2
和p3
变量,因此如果CheckListBox1.Checked[0]
为真,那么{{将分配1}}和p2
,如果p3
也为真,那么CheckListBox1.Checked[1]
和p2
仍然会有前一次迭代的值,而它们可能应该为空。尝试像
p3
答案 1 :(得分: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]);
set constant数组为每种症状声明一组疾病。
获取每个症状的结果字符串以及与症状匹配的疾病集:
// 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
您似乎想要一组符合所有检查症状的疾病。最新的更新显示了如何做到这一点。