How do I enter a Menu choice in a statement

4 次查看(过去 30 天)
I am creating a program that prompts a user to choose a choice from a menu and then use that option to insert in an "fprintf" Statement. Here is what I have so far, but as you can see in the bold statement below, it is missing the choice that was chosen by the user in the beginning.
clear
clc
close all
Materials={'Ash';'Hickory';'Maple';'Pine'};
Cost=[4.35 4.95 6.35 3.75]; % Estimated material cost to produce 25 bats from each material chosen
LECost=1.25; % Labor and Energy Cost regardless of material chosen.
BatMat= menu('Please select Bat material', Materials(1), Materials(2), Materials(3), Materials(4));
if BatMat == 1;
fprintf('Ash');
elseif BatMat ==2;
fprintf('Hickory');
elseif BatMat ==3;
fprintf('Maple');
else
fprintf('Pine');
end
BatPrice=input('Please enter selling price per bat of chosen material: $');
NoBats=input('Please enter the total number of bats the manufacturer can produce per week without an upgrade: ');
NoWeeks=input('Please enter the the number of weeks the manufacturer plans to run the bat production machinery in a year: ');
%Calculation
Ttlbats= (NoBats*NoWeeks);
input('Please enter number of ADDITIONAL bats that can be produced in a week with an upgrade: ');
Fixedcost=input('Please enter the fixed cost for the upgrade: $');
VariableCost=(Cost(1)+LECost);
fprintf('Selling price per bat:\t $%0.2f \n', BatPrice);
fprintf('Total Variable Cost per bat: \t $%0.2f \n', VariableCost);
fprintf('NO UPGRADE \n');
profit=(NoBats*BatPrice);
fprintf('Producing %0.0f _%s_ bats a week for %0.0f weeks = %0.0f total bats\n\t Profit \t\t$%0.0f\n' ,NoBats,BatMat,NoWeeks,Ttlbats,profit) %this line was marked

回答(2 个)

Walter Roberson
Walter Roberson 2017-11-10
Your code
BatMat= menu('Please select Bat material', Materials(1), Materials(2), Materials(3), Materials(4));
if BatMat == 1;
shows us that you know that the result of menu() is the item index. But
fprintf('Producing %0.0f _%s_ bats a week for %0.0f weeks = %0.0f total bats\n\t Profit \t\t$%0.0f\n' ,NoBats,BatMat,NoWeeks,Ttlbats,profit) %this line was marked
shows us that you now expect BatMat (the index) to be the string.
On that line you need to change BatMat to materials{BatMat}
  1 个评论
Juan Cervantes
Juan Cervantes 2017-11-10
编辑:Juan Cervantes 2017-11-10
I ended up taking out the if statements and changed the line to materials{BatMat}, which worked perfectly! Thank you!

请先登录,再进行评论。


Swathi Kulkarni
Swathi Kulkarni 2020-10-6
what is the ouput of this ?please help
clear
clc
S=[2012,27,17;2011,24,13;2010,29,7];
fprintf(‘Clemson=USC Football Rivalry:\n\n’);
fprintf(‘Year\tUSC\t\tClesmon\n’);
fprintf(‘%0.0f\t%0.0f\t\t%00f\n’,S(1,1),S(1,2),S(1,3));
fprintf(‘%0.0f\t%0.0f\t\t%00f\n’,S(2,1),S(2,2),S(2,3));
fprintf(‘%0.0f\t%0.0f\t\t%00f\n’,S(3,1),S(3,2),S(3,3));

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by