How would i make a loop where if a positive number isnt inputted, The question is asked again?

댓글 수: 4

Jan
Jan 2022년 10월 17일
This sounds like a homework assignment. What have you tried so far? Which explicit problem to you have?
Benjamin
Benjamin 2022년 10월 17일
편집: Benjamin 2022년 10월 17일
@Jan here is my code so far, i was trying to create a quadratic equation solver:
disp('Solving a quadratic polynonial (ax^2+bx+c), where a b and c are real numbers to 3 decimal places')
a = input ('What is your value for a?: '); %coefficient of x^2
a =~ NaN;
while isnan(a)
a = input ('Please only enter a numerical value for a: ');
end
but my NaN loop isnt working correctly. I've just learnt about the while function and i was trying to implement a while loop that loops when anything other than a number is inputted
Davide Masiello
Davide Masiello 2022년 10월 17일
편집: Davide Masiello 2022년 10월 17일
Your code seems fine, just remove the line
a =~ NaN;
and it works, meaning that if you input a = nan then it'll display the 'Please only enter a numerical value for a: ' message.
Benjamin
Benjamin 2022년 10월 17일
@Davide Masiello is there a way for this code to work if any letter is inputted as my value of a

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

 채택된 답변

Davide Masiello
Davide Masiello 2022년 10월 17일
편집: Davide Masiello 2022년 10월 17일

1 개 추천

disp('Solving a quadratic polynonial (ax^2+bx+c), where a b and c are real numbers to 3 decimal places')
a = input ('What is your value for a?: '); %coefficient of x^2
while ~isnumeric(a) || isnan(a)
a = input ('Please only enter a numerical value for a: ');
end
Solving a quadratic polynonial (ax^2+bx+c), where a b and c are real numbers to 3 decimal places
What is your value for a?: nan
Please only enter a numerical value for a: 'a'
Please only enter a numerical value for a: 2
>>

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

질문:

2022년 10월 17일

댓글:

2022년 10월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by