Printing a variable within an input command
16 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I am trying to use an input command to tell a specific player that it is there turn to make a move.
My code looks like this:
move = input(['Player ' current_player ', choose column 1-7 to drop a chip: ']);
The output looks like this:
Player , choose column 1-7 to drop a chip:
Can someone help me get this working correctly?
0 Kommentare
Antworten (2)
Star Strider
am 11 Nov. 2019
Assuming ‘current_player’ is a character array or string variable:
move = input(sprintf('Player %s, choose column 1-7 to drop a chip: ', current_player));
Otherwise, choose the appropriate format descriptor in formatSpec for the ‘current_player’ variable class.
0 Kommentare
Image Analyst
am 11 Nov. 2019
I think current_player is an integer, and that's why when you create a string like this:
['Player ' current_player ', choose column 1-7 to drop a chip: ']
it's making non-printable characters. Like if current_player was some number less than ASCII 32. You need to use %d or num2str
move = input(sprintf('Player %s, choose column 1-7 to drop a chip: ', current_player));
Alternatively if you don't like sprintf() for some reason, you can use brackets, quotes, and num2str() to create a character array
userPrompt = ['Player ', num2str(current_player), ' choose column 1-7 to drop a chip: '];
move = input(userPrompt);
0 Kommentare
Siehe auch
Kategorien
Mehr zu Variables finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!