Can you guys help write a code for my machine problem

Write a script that will prompt the user to input a number from 1 – 9 only and display the
figure below.

3 个评论

What happened to the prior post on this same problem?
I deleted it i thought i asked the wrong problem
Nah. I don't think you did. I just wanted to make sure that someone else didn't delete it.
FWIW, when posting things that look like homework, most people here are going to look for some indication that the poster has made an effort to solve the problem at hand. It's not just a matter of proving you did the work; it tells us where you are in the problem solving process and it makes for less redundant discussion.
In the future, just post or attach your current attempt along with your question, even if it's incomplete. Homework questions by themselves get little traction.

请先登录,再进行评论。

 采纳的回答

Since you've already demonstrated that you have actually made an (almost) working solution of your own, I'll offer this as a more succinct approach. You can avoid needing routines for each case by generating the string programmatically.
a = 5; % use your input command or whatever here
a = min(max(a,1),9); % or handle these cases with an error or something
for b = [a:-1:2 1:a]
outerpad = repmat(' ',[1 9-(a-b+1)]);
if b==a
fprintf('%s%d%s\n',outerpad,b,outerpad)
else
innerpad = repmat(' ',[1 17-(2*numel(outerpad)+2)]);
fprintf('%s%d%s%d%s\n',outerpad,b,innerpad,b,outerpad)
end
end
I don't still have a copy of your original code to tell you specifically how the indexing was wrong. Maybe the indexing here would help as an example.

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品

版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by