multiple loopings in a program

조회 수: 1 (최근 30일)
badrul hisham
badrul hisham 2016년 4월 9일
댓글: badrul hisham 2016년 4월 9일
Hi, i have a coding that requires user to input 2 numbers that are within a specific range.for example, for the first number:(please choose a number ranging from 1 to 7). If the user chose a number that is out of range, it will prompt for another number until the number chosen is within range.Then the program will proceed and ask for the second number:(please choose a number ranging from 15 to 20). Same as before, the number must be within range, otherwise it will just keep asking for a number. Can you please show me how to this?
Thank you

채택된 답변

Kuifeng
Kuifeng 2016년 4월 9일
편집: Kuifeng 2016년 4월 9일
a = input('Input the value of a: ');
Range1 = [1 7];
str = strcat('Input the value of a again as a needs to be in the range from ', ...
num2str(Range1(1)),' to ', num2str(Range1(2)),'. a:');
while or(a<Range1(1), a>Range1(2))
a = input(str);
end

추가 답변 (1개)

Walter Roberson
Walter Roberson 2016년 4월 9일
편집: Walter Roberson 2016년 4월 9일
while true
num = input('get on with the first number');
if numels(num) ~= 1 || num < 1 || num > 7
fprintf('Oh, you.. .you!\n');
else
break;
end
end
  댓글 수: 1
badrul hisham
badrul hisham 2016년 4월 9일
undefined function numels?

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

카테고리

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