Why do I keep getting a parse error?

2 次查看(过去 30 天)
Hi Matlabers!
I am trying to make code to run the blackjack card game on Matlab as a class project and keep running into a parse error. I want to label the cards such as AS for "ace of spades", 2S for "two of spades", and so forth for the rest of the cards... In the code, I write the following:
AS=1 2S=2 3S=3
I continue the code for the rest of the cards but omit that here for the sake of brevity. When I try to run the code, AS=1 has no problem but 2S=2 and 3S=3 have a parse error that reads as:
Error: File: Project.m Line: 8 Column: 2 Unexpected MATLAB expression.
I see that I can get around this by renaming the variables as spade2=2 and spade3=3, etc. but this doesn't look as nice to me and would rather keep it as I was originally trying to do. OR even better, I would love it if I can replace the S in 2S or 3S with a picture of an actual spade. The same goes for a diamond, club, and heart for the rest of the cards. I'm hoping there is someone here that knows what to do!
P.S. I would also like AS "ace of spades" to take on the value of either 1 or 11 given the choice of the user. I know that I can write something such as:
AS = input( 'Enter value for Ace: ');
but doing this would allow the user to input any number they want. I would like to give them the input option, but only allow either a value of 1 or 11.
Thank's Matlabers!
  2 个评论
KSSV
KSSV 2018-4-10
2S=2 3S=3
The above is not allowed......you may use
S2=2 ; S3=3 ; instead

请先登录,再进行评论。

回答(1 个)

Walter Roberson
Walter Roberson 2018-4-10
There is no possibility of naming a variable beginning with a number. You must have a letter as the first character. Only letters and digits and underscore are permitted.

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by