簡體   English   中英

if / elseif命令無法正常工作

[英]if/elseif command not working properly

尊敬的StackOverflow社區,

因為我已經經歷了您一次又一次地尋求幫助的意願,所以我再次來到這里尋求幫助。

到目前為止,我有一個包含“客戶”的模擬器。 我想通過分配各種客戶類型(作為屬性)對它們進行分類。 在一定程度上,該分配工作正常,但不知何故,我的代碼中仍然存在錯誤。 但是自己看看:

主功能

ncust = 500 %//(number of customers)

%function call

for ii = 1:ncust;
    customers{ii} = Customer(ii);
    customers{ii}.setcustTypeDistrib(ncust);
end

每個客戶都有一個名為“ id”的屬性(客戶1的ID為1->誰會猜到?)

類客戶

function obj = setcustTypeDistrib(obj, ncust) %sets types of customers
    if obj.id < ceil(ncust/2) % 50 percent of the customers are type 1
        obj.custType = 1;

    elseif ceil(ncust/2) < obj.id || obj.id < ceil(0.75*ncust)
        obj.custType = 2;

    elseif ceil(0.76*ncust) < obj.id 
        obj.custType = 3;
    end
end

但是,例如,如果我檢查客戶450(在MATLAB中: customers{450} ),則他是客戶2,盡管他應該是客戶3。我在做什么錯? 我感到完全迷失了!


例如編輯

具有屬性的客戶:

id: 490
custType: 2
minInterval: 4.7846e+04

問題是您在ceil(ncust/2) < obj.id || obj.id < ceil(0.75*ncust)中使用了“或”語句。 ceil(ncust/2) < obj.id || obj.id < ceil(0.75*ncust)因此由於ceil(ncust/2) < obj.id ,對象ID超過一​​半的所有內容都將是類型2。 使用“和”語句,您將獲得所需的結果。

例如

ceil(ncust/2) < obj.id && obj.id < ceil(0.75*ncust)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM