Ending while loop with an input from the user

조회 수: 10 (최근 30일)
Hani Kerem TURKOGLU
Hani Kerem TURKOGLU 2021년 1월 22일
댓글: Hani Kerem TURKOGLU 2021년 1월 26일
Guys as i beginner at coding, i need help with ending while loop when the user type 'q'. My codes are below.
I appreciate your help. Thanks!
clc
clear
syms q
syms a
while a ~= 'q'
a = input('Lutfen tam bolenlerini bulmak istediginiz sayiyi giriniz:')
disp('cikmak icin q tusuna basin.')
tam_bolenler = tambolenler (a)
end
function tambo = tambolenler (a)
if a > 0
y = 1;
tambo(1,y) = [0];
for i=1:a
if (mod(a,i)==0)
tambo(1,y) = i;
y = y+1;
end
end
elseif a < 0
y = 1;
tambo(1,y) = [0];
for i=-a:-1:a
if (mod(a,i)==0)
tambo(1,y) = i;
y = y+1;
end
end
else a==0
print("Bu program 0 degeri icin sonuc vermez...")
end
end

채택된 답변

Benjamin Kraus
Benjamin Kraus 2021년 1월 23일
편집: Benjamin Kraus 2021년 1월 23일
I'm going to ignore most of the tambolenler function, because I don't think it is needed to answer your question.
I think you have a few changes you need to make:
  1. Remove the calls to syms q and syms a which are probably not doing what you think they are doing.
  2. Initialize a as anything you want other than 'q'.
  3. You need to tell the input command not to evaluate the user's input before returning the answer to you by adding the s flag, then separately use eval to evaluate the user input.
  4. Switch from using ~= to ~isequal. The difference is that ~= is an element-by-element comparison while isqual compares the entire input variables.
a = '';
while ~isequal(a,'q')
a = input('Lutfen tam bolenlerini bulmak istediginiz sayiyi giriniz:','s');
disp('cikmak icin q tusuna basin.')
if a ~= 'q'
a = eval(a); % Convert from a character vector to a number.
tam_bolenler = tambolenler(a);
end
end
  댓글 수: 3
Benjamin Kraus
Benjamin Kraus 2021년 1월 25일
Happy to help. In the future, I suggest using the code formatting tools when posting your question. It makes the code much easier to read, and therefore you are much more likely to get a faster response.
Hani Kerem TURKOGLU
Hani Kerem TURKOGLU 2021년 1월 26일
Yes you are right :) Thank you very much!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by