在下面的代碼中,我檢查第一個字母是否在單詞詞典中,以及單詞的長度是否匹配。如果有,請返回該單詞。否則,返回一個錯誤語句。在Matlab中驗證循環後的顯示結果
words = {'apple', 'banana', 'bee', 'salad', 'corn', 'elephant', 'pterodactyl'};
user_letter_input = input('Please enter the first letter of a word: ', 's');
user_num_input = input('Please enter how long you would like the word to be: ');
for i = words
if ((i{1}(1) == user_letter_input) && (length(i{1}) == user_num_input))
result = i;
else
result = 0;
end
end
if (result == 0)
disp('There are no matching words');
else
disp(['Your new word is: ' result]);
end
比較返回i
是'apple'
如果我用於第二輸入所述第一輸入和鍵入5
a
- 因爲它應該。
然而,在結束的時候我嘗試看看if (result == 0)
,它不會顯示這個新詞,即使result
不爲0
有人能幫助我解決這個問題嗎?
這也將處理多個匹配。 – beaker