2013-03-27 98 views
-1

我需要詢問用戶溫度,k或K代表開爾文,c或C代表攝氏溫度,f或F代表華氏溫度。我怎樣才能把所有這些放在一個循環中?我需要不斷詢問用戶,直到他們輸入上面的一個字母。這是我迄今爲止所擁有的。Matlab,有關循環的基礎知識

tempType = input('What type of temperature would you like to use: ', 's'); 

    value = ['k','K','c','C','f','F']; 

    while strcmp(tempType, value) == 1 

    tempType = input('What type of temperature would you like to use: ', 's'); 

    end 

回答

0

你想(length(tempType)~=1 || length(findstr(tempType,value))==0)成爲你而

1

我寧願做這樣以避免出現兩個完全相同的行(在R2011b測試)的條件:

AllowedTemperature = {'k','K','c','C','f','F'}; 
SelectedTemperature = ''; 
while ~any(strcmp(SelectedTemperature,AllowedTemperature)) 
    SelectedTemperature = input('What type of temperature would you like to use: ', 's'); 
end 
disp([ 'SelectedTemperature: ' SelectedTemperature ]) 
+0

'〜任何( strcmp(....))''可以替換成'〜ismember(SelectedTemperature,AllowedTemperature)' – erikced 2013-03-28 19:32:01