Why is my MATLAB code not executing properly?

1 visualización (últimos 30 días)
whyamiincollege
whyamiincollege el 29 de Abr. de 2019
Comentada: Raj el 30 de Abr. de 2019
Everytime it asks me to choose a number btwn 1-100 and when i do, lets say i choose 53, it will display "so sad :( you have to go to class" then proceed to enter a number for what i want for breakfast but it should proceed to ask what path i want to go to school. Also, i cant press "run" to run my code i have to press f9 or copy and paste it into the command window. Please help, thank you!
clc;
disp('Its 7am wake up!')
n = input('Choose a number 1-100 to start your day ')
if n == 100
disp('Congratulations you get to sleep for the rest of the day')
elseif n > 50
disp('So sad :( you have to get ready for class')
elseif n > 20 disp('Sleep in for 20 more mins')
elseif n > 0 disp('Screw school, Im skipping today!')
end
for n = 0:20
disp('Enter a number for what you want for breakfast')
breakfast = input('1 burrito 2 cereal 3 nothing')
if breakfast == 1
disp('Good choice, your day will be prosperous')
elseif breakfast == 2
disp('Peanut butter capn crunch it is')
elseif breakfast == 3
disp('Not a good choice you will probably die today')
end
end
for n = 51:99
disp('Enter a number for what path will I take to school')
path = input('1 for linear path 2 for exponential path')
if path == 1
x1 = linspace(0,5,.2)
y1 = 2*x1 + 7
plot (x1,y1)
elseif path == 2
for x2 = 0:.2:5
y2 = x2^2
plot (x2,y2)
end
end
end

Respuestas (1)

Raj
Raj el 29 de Abr. de 2019
Hi,
1) In your first if-else part of code, I feel it is better to add a default condition also to take care of any value out of valid range like this:
if n == 100
disp('Congratulations you get to sleep for the rest of the day')
elseif n > 50
disp('So sad :( you have to get ready for class')
elseif n > 20 disp('Sleep in for 20 more mins')
elseif n > 0 disp('Screw school, Im skipping today!')
else
disp("Invalid entry") % exit the code if user inputs invalid value
end
2) Instead of
for n = 0:20
you should use
if n >0 && n<=20
3) Instead of
for n = 51:99
you should use
if n >50 && n<100
4) "RUN" is working perfectly for me. I am using 2016a version.
  2 comentarios
whyamiincollege
whyamiincollege el 29 de Abr. de 2019
So i fixed it to what you said and RUN still isn't working. I highlighted it and pressed f9 and its still displaying this :
breakfast =
[]
Enter a number for what you want for breakfast
1 burrito 2 cereal 3 nothing
Raj
Raj el 30 de Abr. de 2019
This is the modified code:
clc;
disp('Its 7am wake up!')
n = input('Choose a number 1-100 to start your day ')
if n == 100
disp('Congratulations you get to sleep for the rest of the day')
elseif n > 50
disp('So sad :( you have to get ready for class')
elseif n > 20 disp('Sleep in for 20 more mins')
elseif n > 0 disp('Screw school, Im skipping today!')
else
disp('Invalid entry')
end
if n >0 && n<=20
disp('Enter a number for what you want for breakfast')
breakfast = input('1 burrito 2 cereal 3 nothing')
if breakfast == 1
disp('Good choice, your day will be prosperous')
elseif breakfast == 2
disp('Peanut butter capn crunch it is')
elseif breakfast == 3
disp('Not a good choice you will probably die today')
end
end
if n >50 && n<100
disp('Enter a number for what path will I take to school')
path = input('1 for linear path 2 for exponential path')
if path == 1
x1 = linspace(0,5,.2)
y1 = 2*x1 + 7
plot (x1,y1)
elseif path == 2
for x2 = 0:.2:5
y2 = x2^2
plot (x2,y2)
end
end
end
Works fine. "RUN" is also working fine for me. Which version are you using?

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Productos


Versión

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by