PDA

Просмотр полной версии : [Помогите!] Как упростить...


megbum
06.07.2012, 16:54
Можно ли как-нибудь упростить эти строчки, чтобы меньше кода было?

if (sComboBox1.Text=sComboBox1.Items[0]) and(sComboBox2.Text=sComboBox2.Items[0]) then ShowMessage('Нужно 30 монет!');
if (sComboBox1.Text=sComboBox1.Items[0]) and(sComboBox2.Text=sComboBox2.Items[1]) then ShowMessage('Нужно 63 монет!');
if (sComboBox1.Text=sComboBox1.Items[0]) and(sComboBox2.Text=sComboBox2.Items[2]) then ShowMessage('Нужно 99 монет!');
if (sComboBox1.Text=sComboBox1.Items[0]) and(sComboBox2.Text=sComboBox2.Items[3]) then ShowMessage('Нужно 139 монет!');
if (sComboBox1.Text=sComboBox1.Items[0]) and(sComboBox2.Text=sComboBox2.Items[4]) then ShowMessage('Нужно 183 монет!');
if (sComboBox1.Text=sComboBox1.Items[0]) and(sComboBox2.Text=sComboBox2.Items[5]) then ShowMessage('Нужно 231 монет!');
if (sComboBox1.Text=sComboBox1.Items[0]) and(sComboBox2.Text=sComboBox2.Items[6]) then ShowMessage('Нужно 284 монет!');
if (sComboBox1.Text=sComboBox1.Items[0]) and(sComboBox2.Text=sComboBox2.Items[7]) then ShowMessage('Нужно 342 монет');
if (sComboBox1.Text=sComboBox1.Items[0]) and(sComboBox2.Text=sComboBox2.Items[8]) then ShowMessage('Нужно 406 монет!');
if (sComboBox1.Text=sComboBox1.Items[0]) and(sComboBox2.Text=sComboBox2.Items[9]) then ShowMessage('Нужно 477 монет!');
if (sComboBox1.Text=sComboBox1.Items[0]) and(sComboBox2.Text=sComboBox2.Items[10]) then ShowMessage('Нужно 555 монет!');

D.E.Z.E.R.T.I.R
06.07.2012, 17:02
Где много кода? Не вижу.

megbum
06.07.2012, 17:20
Где много кода? Не вижу.
Ну в каждем ComboBox'е по 50 значений
тоесть к 1 значению первого комбобокса будет из второго 50
и так подумать 2500 строчек будет:noemotion:

Sinyss
06.07.2012, 17:22
С твоим подходом - нет.
Откуда берется количество необходимых монет?

R.A.Z.O.R.
06.07.2012, 18:17
Можно ли как-нибудь упростить эти строчки, чтобы меньше кода было?

if (sComboBox1.Text=sComboBox1.Items[0]) and(sComboBox2.Text=sComboBox2.Items[0]) then ShowMessage('Нужно 30 монет!');
if (sComboBox1.Text=sComboBox1.Items[0]) and(sComboBox2.Text=sComboBox2.Items[1]) then ShowMessage('Нужно 63 монет!');
if (sComboBox1.Text=sComboBox1.Items[0]) and(sComboBox2.Text=sComboBox2.Items[2]) then ShowMessage('Нужно 99 монет!');
if (sComboBox1.Text=sComboBox1.Items[0]) and(sComboBox2.Text=sComboBox2.Items[3]) then ShowMessage('Нужно 139 монет!');
if (sComboBox1.Text=sComboBox1.Items[0]) and(sComboBox2.Text=sComboBox2.Items[4]) then ShowMessage('Нужно 183 монет!');
if (sComboBox1.Text=sComboBox1.Items[0]) and(sComboBox2.Text=sComboBox2.Items[5]) then ShowMessage('Нужно 231 монет!');
if (sComboBox1.Text=sComboBox1.Items[0]) and(sComboBox2.Text=sComboBox2.Items[6]) then ShowMessage('Нужно 284 монет!');
if (sComboBox1.Text=sComboBox1.Items[0]) and(sComboBox2.Text=sComboBox2.Items[7]) then ShowMessage('Нужно 342 монет');
if (sComboBox1.Text=sComboBox1.Items[0]) and(sComboBox2.Text=sComboBox2.Items[8]) then ShowMessage('Нужно 406 монет!');
if (sComboBox1.Text=sComboBox1.Items[0]) and(sComboBox2.Text=sComboBox2.Items[9]) then ShowMessage('Нужно 477 монет!');
if (sComboBox1.Text=sComboBox1.Items[0]) and(sComboBox2.Text=sComboBox2.Items[10]) then ShowMessage('Нужно 555 монет!');


Если я правильно понял, что ты проверяешь, какой пункт выбран в комбобоксе, то в данном случае можно упростить так:


if (sComboBox1.ItemIndex = 0) then
begin
case sComboBox2.ItemIndex of
0: ShowMessage('Нужно 30 монет!');
1: ShowMessage('Нужно 63 монет!');

// и т.д.

end;
end;

aktivizion15
06.07.2012, 23:09
или можно по-другому:
в комбобокс дописать так:
вариант1 (30)
вариант2 (63)
и спарсить

showmessage(copy(combobox2.Text,pos('(',combobox2. Text),pos(')',combobox2.Text)));


procedure TForm1.ComboBox1Change(Sender: TObject);
begin
showmessage('нужно '+
copy(combobox1.Text,
pos('(',combobox1.Text)+1,
pos(')',combobox1.Text)-pos('(',combobox1.Text)-1)+' монет!');
end;
xD

VeTaL_UA
07.07.2012, 15:30
ItemIndex? не,не слышал /dgs

Рэйзор
07.07.2012, 15:45
Чубарые, все же можно в одну строку написать. ООП для кого придумали? Нет чтоб учебник прочитать, пишут черти че.
for i:=0 to ComponentsCount-1 do
if Components[i] is TsComboBox then // вариант 2 - if Components[i].Tag = 2 (присвоить всем нужным боксам тег - 2)
... траляля ...

Ну а тут :
var msg:string;
begin
if sComboBox1.ItemIndex = 1 then
Case sComboBox2.ItemIndex of
0: msg:='123';
1: msg:='trololo';
.......
end;
end;