フィルターのクリア

Illegal use of reserved keyword "elseif" keep showing up?

5 ビュー (過去 30 日間)
mohammad
mohammad 2023 年 11 月 9 日
コメント済み: mohammad 2023 年 11 月 9 日
function isortvector
A = [9 -7 8 4 -3 7]
inp = input("enter 0 for ascendig && 1 for decending");
ascending = [];
descendig = [];
if inp == '0'
for i = 1:6
x = A(1);
for j=1:length(A)
if A(j) < x
x=A(j);
end
end
ascending(end+1)=x; A(A==x)=[];
end
disp("Ascending:"); disp(ascending)
end
elseif inp =='1'
for i = 1:6
x=A(1);
for j=1:length(A)
if A(j) > x
x = A(j);
end
descending(end+1) = x; A(A==x)=[];
end
disp("Descending:");
disp(descendig)
disp("uncorect input:")
disp(A)
end
end
[SL: formatted code as code]
  1 件のコメント
Stephen23
Stephen23 2023 年 11 月 9 日
編集済み: Stephen23 2023 年 11 月 9 日
Note that as you have called it INPUT will return numeric values. If you expect characters (as you show use with IF and ELSEIF) then you will need to use the 's' option:
inp = input("enter 0 for ascendig && 1 for decending","s");
% ^^^
You also have to fix the names of some variables.

サインインしてコメントする。

採用された回答

Stephen23
Stephen23 2023 年 11 月 9 日
編集済み: Stephen23 2023 年 11 月 9 日
If you align your code consistently then the problem is very clear:
if inp == '0'
..
end % <- delete this
elseif inp =='1'
..
end
Tip: to align code automatically select all of your code and press ctrl+i.
  1 件のコメント
mohammad
mohammad 2023 年 11 月 9 日
Thank you very much sir.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by