Trying to write a function with random, input and fprint

I am trying to write a function that asks for names, for example it should first ask, What are the names of the players. Then is should pick one name with rand and decide if the player should play goalkeeper, defencer, midfielder og striker. Then it should print out for example... David is going to play as a striker. I have tried everything but i dont get it to work!

2 comentarios

You should show an example of what you have tried
Hlynur
Hlynur el 6 de Dic. de 2012
Editada: Hlynur el 6 de Dic. de 2012
Nafn={'Arnar' 'Hlynur' 'Ingimar' 'Stebbi'};
Drykkur={'Bjór' 'Skot' '2 sopar' 'Ekkert' '1 sopi' 'Hálfur bjór'};
v = randi(length(Nafn));
f = Drykkur(randi(length(Drykkur)));
fprintf('Meistari %s á að drekka %s og vera glæsilegur!\n', v, f);
This is one version of what i have tried

Iniciar sesión para comentar.

 Respuesta aceptada

Try it this way:
Nafn={'Arnar' 'Hlynur' 'Ingimar' 'Stebbi'};
Drykkur={'Bjór' 'Skot' '2 sopar' 'Ekkert' '1 sopi' 'Hálfur bjór'};
button = menu('Enter a player name', Nafn);
message = sprintf('%s is a %s', Nafn{button}, Drykkur{button});
uiwait(msgbox(message));

4 comentarios

Hlynur
Hlynur el 7 de Dic. de 2012
This is in the right direction but not 100% I also want to randomise the "Nafn".... :)
@Hlynur: Do you mean something like this:
Nafn = Nafn(randperm(4))
?
Try it this way:
Drykkur={'Bjór' 'Skot' '2 sopar' 'Ekkert' '1 sopi' 'Hálfur bjór'};
numberOfPositions = length(Drykkur);
for p = 1 : numberOfPositions
% Ask user for a name.
titleBar = 'Enter a player name';
userPrompt = 'Enter a player name';
ca = inputdlg(userPrompt, titleBar, 1);
if isempty(ca)
numberOfPositions = p-1;
break;
end
Nafn(p) = ca;
end
% Randomize the names of the players.
Nafn = Nafn(randperm(numberOfPositions))
message = '';
for p = 1 : numberOfPositions
message = sprintf('%s\n%s is a %s', ...
message, Nafn{p}, Drykkur{p});
end
uiwait(msgbox(message));
Hlynur
Hlynur el 7 de Dic. de 2012
Thanks alot!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre View and Analyze Simulation Results en Centro de ayuda y File Exchange.

Preguntada:

el 6 de Dic. de 2012

Community Treasure Hunt

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

Start Hunting!

Translated by