필터 지우기
필터 지우기

How to make a conditional statement for blood types?

조회 수: 8 (최근 30일)
Brandon
Brandon 2023년 3월 2일
편집: Cameron 2023년 3월 2일
I tried to make a conditional statement so that when someone enters their blood type, they would get the results from which blood type they can receive and which they can donate to, I haven't finished it but when I type "A+" to answer the question it gives me the error message below. Can someone explain how to fix this for me.
clc,clear
bloodtype = ["A+", "A-", "AB+", "AB-", "B+", "B-", "O+", "O-", "everyone"];
bloodtype = input('what is your blood type?: ');
if bloodtype == "A"
disp('You can give to A+, and AB+');
disp('You receive from A+, A-, O+, and O-')
end
what is your blood type?: A+
A+
Error: Invalid expression. Check for missing or extra characters.

답변 (1개)

Cameron
Cameron 2023년 3월 2일
편집: Cameron 2023년 3월 2일
When you put a "+" MATLAB is expecting you to perform a mathematical operation. To get around this, you can input put quotes around your input to indicate it is a string such as "A+" or 'A+'. The difference between these is a string vs character array.
This is probably the way I would do it.
Input = ["A+","A-","B+","B-","O+","O-","AB+","AB-"];
Donor = {{"A+","AB+"},{"A-","A+","AB-","AB+"},{"B+","AB+"},{"B-","B+","AB-","AB+"},...
{"O+","A+","B+","AB+"},...
{"All Blood Types"},{"AB+"},{"AB-","AB+"}};
Receive = {{"A+","A-","O+","O-"},{"A-","O-"},{"B+","B-","O+","O-"},...
{"B-","O-"},{"O+","O-"},{"O-"},{"All Blood Types"},{"AB-","A-","B-","O-"}};
[indx,tf] = listdlg('PromptString','Select a blood type',...
'SelectionMode','single','ListString',Input);
if tf == 0; return; end
disp('Your blood type: ' + strjoin(string(Input{indx}),','))
disp('You can give to: ' + strjoin(string(Donor{indx}),','))
disp('You can receive from: ' + strjoin(string(Receive{indx}),','))

카테고리

Help CenterFile Exchange에서 Biological and Health Sciences에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by