how to: multiple choises in if-function
古いコメントを表示
hello,
first of all my english is not the yellow from the egg. I try to improve my english.
I have tried to evaluate the indice of the letter from the alphabet and determine if it is a vowal of a consonant.
This is what i have done and it works:
Erzeugen eines zufaellig ausgewaehlten Buchstaben
% Alphabet erzeugen.
alphabet = ["A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z"]';
% Anzahl der Wahlmöglichkeiten bestimmen.
a = length(alphabet);
% Zufaellige Zahl waehlen.
i = randi(a);
% Zufaelligen Buchstaben waehlen.
zufBuchst = alphabet(i);
% Unterscheidung ob Konsonant oder Vokal und Darstellung, welcher Buchstabe
% es ist.
if i == 1 || i==5 || i==9 || i==15 || i==21;
disp("Der zufaellig ausgewaehlte Buchstabe lautet: '" + zufBuchst + "', steht an " + i + "ter Stelle im Alphabet und ist ein Vokal.")
else disp("Der zufaellig ausgewaehlte Buchstabe lautet: '" + zufBuchst + "', steht an " + i + "ter Stelle im Alphabet und ist ein Konsonant.")
end
But the bold part seems a little bit awkward to me.
have can i make this part shorter and easier?
1 件のコメント
Andreas Goser
2023 年 3 月 7 日
Sie können Fragen gerne in Deutsch stellen. Es wird eine Mischung aus deutschen und englischen Antworten kommen.
回答 (1 件)
looks good, but you could increase readability e.g. by using the ismember function
alphabet = ["A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z"]';
a = length(alphabet);
i = randi(a);
isConsonant=ismember(alphabet(i),["A" "E" "I" "O" "U"])
% or at least, which is a bot more compact
ismember(i,[1 5 9 15 21])
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!