Multiple sum in switch case calculator

조회 수: 7 (최근 30일)
Serguzest
Serguzest 2021년 3월 17일
댓글: Serguzest 2021년 3월 18일
Im trying to write a calculator but i cant take multiple numbers from input operator to sum. How can i ?
  댓글 수: 1
John D'Errico
John D'Errico 2021년 3월 17일
You learn to use vectors? You learn to use loops? Really, you need to learn MATLAB.

댓글을 달려면 로그인하십시오.

채택된 답변

Dave B
Dave B 2021년 3월 17일
I think that what you're asking about is using the input function to gather multiple values, and then passing those values into the sum function. If I'm incorrect, maybe you could give a little more detail in your question
You can accept a vector from 'input' using square brackets:
>> x=input('Enter some numbers, use brackets to specify a vector: ');
Enter some numbers, use brackets to specify a vector: [1 1 2 3 5 8]
>> sum(x)
ans =
20
An alternative design might be to use a loop, keep prompting for more numbers:
Script
x = 0;
next = 0;
prompt = "Enter a number, or hit enter without any value to compute sum: ";
while(~isempty(next))
next = input(prompt);
x=[x next];
end
sum(x)
Run
Enter a number, or hit enter without any value to compute sum: 1
Enter a number, or hit enter without any value to compute sum: 1
Enter a number, or hit enter without any value to compute sum: 2
Enter a number, or hit enter without any value to compute sum: 3
Enter a number, or hit enter without any value to compute sum: 5
Enter a number, or hit enter without any value to compute sum: 8
Enter a number, or hit enter without any value to compute sum:
ans =
20
  댓글 수: 1
Serguzest
Serguzest 2021년 3월 18일
very thanks, that promp code is exactly what i was looking for, greetings

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by