Why do I keep getting a parse error?

조회 수: 2 (최근 30일)
Carlos Hernandez
Carlos Hernandez 2018년 4월 10일
댓글: Carlos Hernandez 2018년 4월 14일
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
Carlos Hernandez
Carlos Hernandez 2018년 4월 14일
Thanks KSSV!

댓글을 달려면 로그인하십시오.

답변 (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