Need to create an array that consists of 5 numbers found through a while loop
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Walter Roberson
le 20 Nov 2024
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.)
Réponse acceptée
Stephen23
le 20 Nov 2024
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);
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Parallel Computing Fundamentals dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!