How to make a conditional statement for blood types?

6 Ansichten (letzte 30 Tage)
Brandon
Brandon am 2 Mär. 2023
Bearbeitet: Cameron am 2 Mär. 2023
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.

Antworten (1)

Cameron
Cameron am 2 Mär. 2023
Bearbeitet: Cameron am 2 Mär. 2023
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}),','))

Kategorien

Mehr zu Biological and Health Sciences finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by