why will the area only work for the circle and not the other shapes

1 回表示 (過去 30 日間)
Jack Purdy
Jack Purdy 2022 年 2 月 2 日
コメント済み: Jack Purdy 2022 年 2 月 2 日
when i run the code and type 'circle' when prompted the code works and finds the area, when i type rectangle or cylinder i get an error message telling me there is a problem with line 3. this is the first time i have used mathlab and my new lecturer in uni only reads his notes to us without teaching us to physically write the code.
any help is much appreciated.
prompt = 'shapes (circle, rectangle, cylinder):';
shapes = input(prompt,'s');
if shapes =='circle'
prompt = 'input r:';
r=input(prompt);
A=pi*r^2;
elseif shapes =='rectangle'
prompt = 'enter (length:)';
length=input(prompt,'s');
prompt= 'enter (breadth:)';
breadth=input(prompt,'s');
A=length*breadth;
elseif shapes=='cylinder'
prompt='enter (height:)';
height=input(prompt,'s');
prompt='enter (r):';
r=input(prompt,'s');
A=(pi*r^2)*height;
end
formatspec= 'value in squared units %10.3f';
fprintf(formatspec,A)
  1 件のコメント
James Tursa
James Tursa 2022 年 2 月 2 日
Please remove your screenshot image and instead post your code as plain text highlighted with the CODE button. We can't copy & run images.

サインインしてコメントする。

回答 (1 件)

James Tursa
James Tursa 2022 年 2 月 2 日
編集済み: James Tursa 2022 年 2 月 2 日
To compare strings, do not use the == operator which does an elementwise compare. Instead, use a function meant for comparing strings. E.g., strcmp( ), strcmpi( ). You could even use isequal( ) in this context.
if strcmp(shapes,'circle')

カテゴリ

Help Center および File ExchangeAnnotations についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by