Need to create an array that consists of 5 numbers found through a while loop

26 次查看(过去 30 天)
I am creating a game where you loop through a while loop and get a random number between set values. If your number is within a range you get points and that is added to your total. I am trying to also display the values (distance) you get in the command window with fprintf but I am getting a struct value for the array. I have tried multiple struct commands and was eventually able to get it to a cell, but was unable to print. I am also unable to get my append to work. So how do I save these values as I loop through my function and print it out.
userInput = input("Which car variation are you testing?\n1. Gatorade wheels, gatorade bottle\n2. Gatorade wheel, water bottle\n" + ...
"3. Water bottle wheels, gatorade bottle\n4. Water bottle wheels, water bottle\n");
score = 0;
if userInput == 1
count = 0;
while count < 5
a = 10;
b = 20;
distance = (b-a).*rand(1,1) + a;
array = [];
array.append = distance;
distance1 = distance(count);
count = count + 1;
if distance >= 15 && distance <= 18
score = score + 3;
elseif distance < 18
score = score + 1;
elseif distance < 15
score = score + 1;
end
end
elseif userInput == 2
elseif userInput == 3
elseif userInput == 4
else
printf("Sorry, we could not perform your test.");
end
fprintf("You travelled %d and your total score is: %3.0f", array, score);
  3 个评论
Jacob
Jacob about 14 hours 前
I did the distance1 thing as a lets see what this does and proceed I just forgot to erase it before i asked this question. I fixed the array = []; and the array(end+1) but im getting an array size of 1x10 instead of 5 do you know why this could be.
Walter Roberson
Walter Roberson about 18 hours 前
if distance >= 15 && distance <= 18
score = score + 3;
elseif distance < 18
score = score + 1;
elseif distance < 15
score = score + 1;
end
Consider a distance value < 15. It would fail the >= 15 & <= 18 test so it would move to the elseif. Since it is < 15 it is certainly < 18 so it would pass the < 18 test. Having succeeded on that test, it would not use the "elseif" comparison against 15.
I suspect the desired test is elseif distance > 18
Note that if the requirement is for the score to be incremented by 1 whenver the distance is outside the range 15 to 18, then you could do that with a simple "else" (unless somehow the distance can end up being nan in which case specific tests for > 18 and < 15 would exclude the nan case.)

请先登录,再进行评论。

采纳的回答

Stephen23
Stephen23 about 15 hours 前
Take a look at this line:
array.append = distance;
You looks as if you are trying to write Python code. However, MATLAB does not have lists and it certainly does not have methods (e.g. APPEND) that operate on lists. The MATLAB approach is to use indexing:
userInput = 1;
score = 0;
array = [];
if userInput == 1
count = 0;
while count < 5
a = 10;
b = 20;
distance = (b-a).*rand(1,1) + a;
array(end+1) = distance;
count = count + 1;
if distance >= 15 && distance <= 18
score = score + 3;
elseif distance < 18
score = score + 1;
elseif distance < 15
score = score + 1;
end
end
elseif userInput == 2
elseif userInput == 3
elseif userInput == 4
else
printf("Sorry, we could not perform your test.");
end
fprintf("You travelled %g and your total score is: %u", array(end), score);
You travelled 17.062 and your total score is: 10

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Parallel Computing Fundamentals 的更多信息

标签

产品


版本

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by