Why is my MATLAB code not executing properly?

1 次查看(过去 30 天)
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

回答(1 个)

Raj
Raj 2019-4-29
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 个评论
whyamiincollege
whyamiincollege 2019-4-29
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 2019-4-30
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?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by