Help with matrix that stores info/interactive?

2 ビュー (過去 30 日間)
Taylor Gates
Taylor Gates 2018 年 7 月 18 日
コメント済み: Will Fritz 2018 年 7 月 18 日
I'm trying to create a matrix/code for the following stipulations. First, create a matrix that stores the name of products and price. 1st and 2nd column should be the product name and its price, respectively. The number of product items should be flexible. Your program will ask to input product name, and it should display the price by looking at the matrix you defined. If the input product name is not in the list, it will display “Item is not in our store. Please retry.” This query should be repeated until you type in “Bye”.
How can I make a matrix interactive like this?

採用された回答

Will Fritz
Will Fritz 2018 年 7 月 18 日
It is my understanding that you want to create some way of storing product/pricing information with the following stipulations:.
  • There should be no limit on how many products there are
  • The program will ask user to provide a potential product
  • If available the price of that object will be displayed
  • If 'Bye' is entered the program will exit
  • If the input is not valid the program should display 'Item is not in our store. Please retry.'
Based on your criteria I suggest using container.Map. The following example is one way of achieving the above:
%%Creating Map
% populate each variable
% Order determines which price is mapped to the specific product
productNames = {'car', 'basketball', 'pen'};
prices = [5000 20 1];
MyMap = containers.Map(productNames, prices);
% add a value to Map
MyMap('tv') = 300;
%%Using Map with User
readInput = true;
while(readInput)
userResponse = input('Please Input a product name: ', 's');
if strcmp(userResponse, 'Bye')
readInput = false;
else
if isKey(MyMap, userResponse)
disp(['Price for ', userResponse, ': $', num2str(MyMap(userResponse))]);
else
disp('Item is not in our store. Please retry');
end
end
end
Please refer to the container.Map documentation for information on how to operate on maps and the methods that come along with this data type
  2 件のコメント
Taylor Gates
Taylor Gates 2018 年 7 月 18 日
Thanks! Would it be possible to use a matrix for the product/pricing and use switch with input to complete this? That's what I'm trying to work on now.
Will Fritz
Will Fritz 2018 年 7 月 18 日
This is what a matrix would look like. MATLAB calls this particular one a 4x2 string array.
%%Creating Map
% populate each variable
% Order determines which price is mapped to the specific product
productNames = ["car", "basketball", "pen"];
prices = ["5000" "20" "1"];
MyMatrix = [productNames' prices'];
% add a value to Map
MyMatrix(4,1) = "tv";
MyMatrix(4,2) = "400";
What do you have in mind for the switch? The only constant thing it can match with is "Bye", everything else depends on the content of the matrix.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeArgument Definitions についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by