i try to print from (row, column) but I'm keep getting (row,row) for some reason.

1 vue (au cours des 30 derniers jours)
Abusayem Mahfuj
Abusayem Mahfuj le 8 Mai 2021
clf
numberofPlayers=input('Please enter the number of players: ')
guess=0;
count=0;
for i=1:numberofPlayers
name=input('Please enter you name: ','s')
number=randi([1,15])
while guess~=number
guess=input('Please guess a number from 1 to 15: ')
if abs(guess-number)==0
disp('You won!')
count=count+1;
elseif abs(guess-number)==1
disp('You are very close')
count=count+1;
elseif abs(guess-number)==2 || abs(guess-number)==3
disp('You are getting close')
count=count+1;
else
disp('You are not close')
count=count+1;
end
end
str{i}=name;
Guesses(i)= count;
count=0;
end
Names=transpose(string(str));
Attempts=transpose(Guesses);
row=find(Attempts==min(Attempts))
table=[Names,Attempts];
fprintf('Names\t Attempts')
disp(table)
x=numel(row)
fprintf('The winner is %2s with %1s attempts \n',table(row,1),table(row,2))
% Results:
% numberofPlayers = 3
% name = 'Mitch'
% number = 10
% guess = 10
% You won!
% name = 'Pol'
% number = 6
% guess = 6
% You won!
% name = 'Matt'
% number = 8
% guess = 8
% You won!
% row = 3×1
% 1
% 2
% 3
%
% Names Attempts
% "Mitch" "1"
% "Pol" "1"
% "Matt" "1"
% x = 3
% The winner is Mitch with Pol attempts
% The winner is Matt with 1 attempts
% The winner is 1 with 1 attempts
% I'm trying to make it say The winner is Mitch with 1 attempts. The winner is Pol with 1 attempts. The winner is Matt with 1 attempts rather than the above.

Réponses (3)

Walter Roberson
Walter Roberson le 8 Mai 2021
fprintf('The winner is %2s with %1s attempts \n',table(row,1),table(row,2))
You are using string variables, so this kind of task becomes easier if you switch to compose()
disp(compose("The winner is %2s with %1s attempts \n", table(row,1),table(row,2)))

Image Analyst
Image Analyst le 8 Mai 2021
table is a built-in function name. Do not use it as the name of one of your variables. Call it something else, like myMatrix or whatever.

Dyuman Joshi
Dyuman Joshi le 8 Mai 2021
You are getting this result because you have defined tables using horinzontal concatenation.
table = [Names,Attempts];
Change it to this and then run your code
table = [Names;Attempts];
  2 commentaires
Abusayem Mahfuj
Abusayem Mahfuj le 8 Mai 2021
doing so, changes my table however. I do need the numbers on the right side horizontally.
Dyuman Joshi
Dyuman Joshi le 8 Mai 2021
So you need, [Name1 Attempts1 Name2 Attempts ....] like this? Try
table = reshape([Names;Attempts],1,[])
But if you do this, you will have to adjust your fprintf statement accordingly

Connectez-vous pour commenter.

Catégories

En savoir plus sur Interactive Control and Callbacks dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by