MATLAB: Help with while loop programming

randrepetitionwhile loop

1)Add code that repeatedly prompts the user for a classification number, an integer from 1 to 4, until the user enters a valid classification number. Hint: use the floor() function to test for an integer value.
I made this program, but it only gives 0 or 1 and does not show disp
How can I fix this to show disp?
%this function will repeatedly prompt the user for a classification number
%until the user enters a valid classification number
classification = input('Enter your classification as an interger from 1 to 4: ');
TF = (5 > classification) && (classification > 0)
while (5 > classification) && (classification > 0) == 0
disp('INVALID INPUT – the number entered is not a valid classification number');
classification = input ('Please re-enter your classification as an interger from 1 to 4: ');
if (5 > classification) && (classification > 0) == 1
disp(classification)
disp('The interger entered is a valid classification number');
end
end
2)Add code that repeatedly displays a random integer from 1 to 6 (the roll of a die) until the first number displayed is redisplayed. Thus, numbers after the first number may be repeated any number of times, but the first number will be repeated exactly once. Hint: use the following expression to get a random integer from 1 to 6: floor(rand()*6) + 1
I am completely lost with this question

Best Answer

Almost there! Give it a meaningful but not confusion variable name, and don't repeat the same calculation.
%this function will repeatedly prompt the user for a classification number
%until the user enters a valid classification number
classification = input('Enter your classification as an interger from 1 to 4: ');
IsValid = (5 > classification) && (classification > 0);
while ~IsValid
disp('INVALID INPUT - the number entered is not a valid classification number');
classification = input ('Please re-enter your classification as an interger from 1 to 4: ');
IsValid = (5 > classification) && (classification > 0);
if IsValid
disp(classification)
disp('The interger entered is a valid classification number');
end
end
Related Question