what loop to use to make the code\input repeat again ?

67 ビュー (過去 30 日間)
adulrahman alsallum
adulrahman alsallum 2019 年 10 月 16 日
回答済み: adulrahman alsallum 2019 年 10 月 16 日
hello all , i'm new to the matlab and trying to learn so my question is very basic
i have this code for example :
a=5 ;
i = input('choose a number ');
if i == a
disp ('RR');
end
if i~=a
disp ('wrong!' );
end
when the user input 5 it will display a massege , otherwise a diffrenet massege
but it only works once , if i input for example 3 it will display the correct massege , but when i enter any number again nothing happens i have to rerun the code.
how can i fix this ? i want to input any number any number of times with the correct massege appearing without having to rerun the code

採用された回答

Neeraj Kaberpanthi
Neeraj Kaberpanthi 2019 年 10 月 16 日
Hi,
Matlab support two types of loops, while and for. you can use any of them.
for example using while loop
n=input('Enter number of executions you want: ');
while n>0
a=5 ;
i = input('choose a number ');
if i == a
disp ('RR');
end
if i~=a
disp ('wrong!' );
end
n=n-1;% decrrising n by 1 at every loop
end
and whith for loop
n=input('Enter number of executions you want: ');
for j=1:n
a=5 ;
i = input('choose a number ');
if i == a
disp ('RR');
end
if i~=a
disp ('wrong!' );
end
end
for more details on loops follow attached links
hope it will help you to learn MATLAB

その他の回答 (2 件)

Andrei Bobrov
Andrei Bobrov 2019 年 10 月 16 日
while true
a=5 ;
disp(' ');
i = input('choose a number ');
disp(' ');
if i == a
disp ('RR');
else
disp ('wrong!' );
end
disp(' ');
j = input(' Stop? (input: "y" - yes or "n" - no) ','s');
disp(' ');
if strncmp(j,'yes',1)
break
end
end

adulrahman alsallum
adulrahman alsallum 2019 年 10 月 16 日
ty all , it was very helpfull , i thought it would need another loop method .
ty

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by