Repeating loop until input conditions are met

조회 수: 20 (최근 30일)
Soren Lundheim
Soren Lundheim 2020년 3월 25일
댓글: Soren Lundheim 2020년 3월 25일
For my program, I need to input two sides and one angle of a triangle. If the angle input is above 180 degrees, I need to loop back to input a new angle. This process needs to repeat until the condition a1<180 is met.
Here's my program so far, which only prompts the user that the triangle is impossible once
I've looked at other forums on "looping until condition is met", but very few of them deal with inputs, so I don't really know where to begin with a for/while loop.
  댓글 수: 1
Subhamoy Saha
Subhamoy Saha 2020년 3월 25일
편집: Subhamoy Saha 2020년 3월 25일
You have to check whether angle is > 180. If yes then again take input for angle certainly after displaying the message.
take input for angle
start the loop: check whether input value>180
display msg
again take input for angle
end the loop
NB Direct answers cannot be given for textbook problems.

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

채택된 답변

Sriram Tadavarty
Sriram Tadavarty 2020년 3월 25일
편집: Sriram Tadavarty 2020년 3월 25일
Hi,
One small change that can be done is to place a while loop for the a1 input and then if a1 < 180, break the loop.
Prototype code:
while 1
a1 = input('Angle in degrees:');
if a1 < 180
break;
else
disp('Triangle is impossible...');
end
end
Hope this helps.
Regards,
Sriram

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by