how do I program 2 things in 1 function file without loop
1 次查看(过去 30 天)
显示 更早的评论
This function will display a blackjack hand to the command window. Its input argument, hand, will be a row vector containing the hand as described: A blackjack hand is a row vector containing numbers between 1 and 13 representing the cards. The correspondence between numbers and cards is1 = ace, 2 = two, ... 11 = jack, 12 = queen, 13 = king. It has no output argument. The contents of the display_hand()function will be as follows:
a.Define a row vector with characters representing the card values, like so:cards = [‘A’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’, ‘T’, ‘J’, ‘Q’, ‘K’]; The character corresponding to a card value is accessed by indexing. For example if the card is a ten, you can display the corresponding character using the function fprintf(‘%c’,cards(10))
b.The entire hand can be displayed with a single fprintf() call as follows:fprintf(‘%c ’,cards(hand))
c.Once the hand has been displayed, print a new line using fprintf(‘\n’)
How can I make all these 3 conditions in 1 function external file ?
1 个评论
Guillaume
2018-9-19
You're given nearly every single character that needs to be typed in the function in the question, so it's really not clear what problem you're facing.
I suggest you make a start at writing the code and if you do face a problem then come back with what you have written and ask a specific question.
回答(1 个)
Viren Gupta
2018-9-26
You need to define a function in MATLAB with the name 'display_hand'. You can refer here. Your function takes an input a row vector(say v). You have 3 conditions to work on:- a. Defining a row vector ('cards') -
cards=[‘A’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’, ‘T’, ‘J’, ‘Q’, ‘K’];
b. Displaying the blackjack hand -
fprintf('%c',cards(v));
c. Just print a newline character.
I guess this problem is pretty simple and you should be able to do this easily.
3 个评论
Viren Gupta
2018-9-28
You seem to have copied the expression:-
cards=[‘A’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’, ‘T’, ‘J’, ‘Q’, ‘K’];
from some source into the MATLAB editor. Try deleting the single quotes manually and again insert single quotes. While you copied and pasted the expression, single quotes got copied in a format which is not supported in MATLAB. Just insert your own single quotes around each of the 13 characters from your keyboard and it will work fine!
Stephen23
2018-9-28
编辑:Stephen23
2018-9-28
"You seem to have copied the expression...from some source into the MATLAB editor."
Yes, I did indeed. And that "some" source was your answer:
"While you copied and pasted the expression, single quotes got copied in a format which is not supported in MATLAB"
Not at all: your answer does not use single quotes. I just checked the HTML using an online character code lookup tool, and these are the characters that you actually used in your answer:
- LEFT SINGLE QUOTATION MARK U+2018
- RIGHT SINGLE QUOTATION MARK U+2019
They certainly don't look like single quotes to me. Rather intriguingly you did use single quotes around the terms 'display_hand' and 'cards', all correctly encoded as:
- APOSTROPHE U+0027
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Whos 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!