Help with while loop programming

1 view (last 30 days)
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

Accepted Answer

Fangjun Jiang
Fangjun Jiang on 25 Mar 2020
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
  3 Comments
Fangjun Jiang
Fangjun Jiang on 25 Mar 2020
Very similar to this one. Roll the dice once, remember this "first number", then keep rolling the dice till the number rolled is the same as the "first number".
Angel Washington
Angel Washington on 25 Mar 2020
Edited: Angel Washington on 25 Mar 2020
%this function will repeatedly displays a random integer from 1 to 6 (the roll
%of a die) until the first number displayed is redisplayed
firstnumber = floor(rand()*6) + 1
while ~firstnumber
rollagain = floor(rand()*6) + 1
if firstnumber
disp('First number displayed is redisplayed');
end
end
I had something like this earlier, but I figured it was not on the right track

Sign in to comment.

More Answers (0)

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!