How do I enter a Menu choice in a statement
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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
0 comentarios
Respuestas (2)
Walter Roberson
el 10 de Nov. de 2017
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 comentario
Swathi Kulkarni
el 6 de Oct. de 2020
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));
0 comentarios
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!