Help with while loop programming

2 ビュー (過去 30 日間)
Angel Washington
Angel Washington 2020 年 3 月 25 日
編集済み: Angel Washington 2020 年 3 月 25 日
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

採用された回答

Fangjun Jiang
Fangjun Jiang 2020 年 3 月 25 日
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 件のコメント
Fangjun Jiang
Fangjun Jiang 2020 年 3 月 25 日
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 2020 年 3 月 25 日
編集済み: Angel Washington 2020 年 3 月 25 日
%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

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDirect Search についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by