MATLAB Answers

Indexing a Variable in an Input Prompt

조회 수: 2(최근 30일)
Sarah Rasavanh
Sarah Rasavanh 21 Nov 2019
Commented: F.M 24 Nov 2019
I am trying to create and input statement that refrences a changing variable in my code.
Playerturn = 1
prompt = ('Player %i move x: ', Playerturn);
y1 = input(prompt, 's');
I'm able to print the input statement but it prints it as
Player %i move x:
instead of
Player 1 move x:
I have a while loop that changes the value of Playerturn back and forth between 1 and 2, how do I reference the variable in one input statement so that it can work for both players?

  댓글 수: 0

로그인 to comment.

답변 수 (2)

Fangjun Jiang
Fangjun Jiang 21 Nov 2019
prompt = sprintf('Player %i move x: ', Playerturn)

  댓글 수: 1

F.M
F.M 24 Nov 2019
Dear Mr. Fangjun Jiang
How can I contact you?

로그인 to comment.


Steven Lord
Steven Lord 21 Nov 2019
If you're using a release that supports string you can use the + operator for string.
Playerturn = 2;
moveNumber = 1;
y1 = input("Player " + Playerturn + " move " + moveNumber + ": ", 's')

  댓글 수: 0

로그인 to comment.

이 질문에 답변하려면 로그인을(를) 수행하십시오.


Translated by