multiple loopings in a program

1 Ansicht (letzte 30 Tage)
badrul hisham
badrul hisham am 9 Apr. 2016
Kommentiert: badrul hisham am 9 Apr. 2016
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

Akzeptierte Antwort

Kuifeng
Kuifeng am 9 Apr. 2016
Bearbeitet: Kuifeng am 9 Apr. 2016
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

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 9 Apr. 2016
Bearbeitet: Walter Roberson am 9 Apr. 2016
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

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by