Indexing a Variable in an Input Prompt

5 ビュー (過去 30 日間)
Sarah Rasavanh
Sarah Rasavanh 2019 年 11 月 21 日
コメント済み: F.M 2019 年 11 月 24 日
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?

回答 (2 件)

Fangjun Jiang
Fangjun Jiang 2019 年 11 月 21 日
prompt = sprintf('Player %i move x: ', Playerturn)
  1 件のコメント
F.M
F.M 2019 年 11 月 24 日
Dear Mr. Fangjun Jiang
How can I contact you?

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


Steven Lord
Steven Lord 2019 年 11 月 21 日
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')

カテゴリ

Help Center および File ExchangeBoard games についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by