Storing Multiple integer values from the user

11 ビュー (過去 30 日間)
Std1
Std1 2020 年 4 月 11 日
コメント済み: Std1 2020 年 4 月 12 日
Hi, I wrote a code which is in below. I want to add something more complex. I have tried so many times, I did not do that.
I want to calculate the sum of numbers that user entered and also show it as a result.
Please help me about solving this problem.
i=1;
C=logical(1)
fprintf('Please enter the marks for the students \n');
while C
fprintf('Please enter the student marks \n');
grades(i)=input('');
i=i+1;
fprintf('Do you want to enter more marks [Y/N] \n');
c=input('','s');
if c=='Y' || c=='y'
C=1;
else
C=0;
end
end
  1 件のコメント
Std1
Std1 2020 年 4 月 12 日
Thanks it worked

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

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 4 月 11 日
編集済み: Ameer Hamza 2020 年 4 月 11 日
try this.
i=1;
C = true;
fprintf('Please enter the marks for the students \n');
grades = [];
while C
fprintf('Please enter the student marks \n');
grades(i)=input('');
i=i+1;
fprintf('Do you want to enter more marks [Y/N] \n');
c=input('','s');
if c=='Y' || c=='y'
C=1;
else
C=0;
end
end
fprintf('Sum of marks is %d\n', sum(grades));

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by