How to make a conditional statement for blood types?

6 次查看(过去 30 天)
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