2015-06-20 39 views
0

我有我的MATLAB代碼如何通過循環不斷迭代的MATLAB

usrinput = input('Enter Month: ', 's'); 
if strcmp(usrinput, 'July') 
    disp('Summer') 
elseif strcmp(usrinput, 'January') 
     disp('Winter') 
elseif strcmp(usrinput,'October') 
disp('Fall') 
elseif strcmp(usrinput, 'April') 
disp('Spring') 
end 

當您輸入一個月,它給你的季節, 但每次我調用腳本的時間(稱爲月)和輸入一個月,我不得不再次調用腳本再做一個月。 如何設置它,以便我不必每次都調用腳本。 AKA在我輸入七月後,它說冬天,它會自動再次說「輸入月份:」,我可以輸入一個新的月份 謝謝!

回答

0

你試過這個嗎?

n = 10 
while n > 1 
    n = n-1; 
    usrinput = input('Enter Month: ', 's'); 
if strcmp(usrinput, 'July') 
    disp('Summer') 
elseif strcmp(usrinput, 'January') 
     disp('Winter') 
elseif strcmp(usrinput,'October') 
disp('Fall') 
elseif strcmp(usrinput, 'April') 
disp('Spring') 
    end 
end 

有關,而細節是here

1

你可以只用一個無限的「while循環」使用while(1),你也可以選擇使用switch一個更優雅的代碼,這裏是代碼:

while (1) 
    usrinput = input('Enter Month: ', 's'); 
    switch usrinput 
     case 'July' 
      disp('Summer') 
     case 'January' 
      disp('Winter') 
     case 'October' 
      disp('Fall') 
     case 'April' 
      disp('Spring') 
     case 'exit' 
      break 
     otherwise 
      disp('Please enter a month.') 
    end 
end 

循環將一直運行,直到用戶鍵入'exit'。