フィルターのクリア

How to call the second function inside the first function under the same file

4 ビュー (過去 30 日間)
Shuoze Xu
Shuoze Xu 2022 年 4 月 15 日
コメント済み: Shuoze Xu 2022 年 4 月 18 日
% That is my code
% the expected result is :
Please enter the first user name: abby
Please enter the first user name: bob
The first user abby have 15 points
The second user bob have 15 points
% but there are bug on my code
function [p1,p2,Name1,Name2]= Playerpoint(point1,point2)
point1 = 10;
point2 = 11;
p1 = card1 + 5;
p2= card2 + 4;
Name1 = name1;
Name2 = name2;
fprintf("The first user %s has %d points\n",Name1,c1);
fprintf("The second user has %d points\n",Name2,c2);
end
function [name1,name2] = Playername(n1,n2)
name1 = n1;
name2 = n2;
end
% driver files
n1 = input('Please enter the first user name: ','s');
n2 = input('Please enter the second user name: ','s');
[name1,name2] = Playername(n1,n2);
card1 = 0;
card2 = 0;
[p1,p2,Name1,Name2]= Playerpoint(point1,pointt2);
% How to improve my code
% Thank you

採用された回答

Image Analyst
Image Analyst 2022 年 4 月 15 日
You don't need the second function. And your arguments were all messed up.
Try the attached.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
% That is my code
% The expected result is :
% Please enter the first user name: abby
% Please enter the first user name: bob
% The first user abby have 15 points
% The second user bob have 15 points
n1 = input('Please enter the first user name: ','s');
n2 = input('Please enter the second user name: ','s');
card1 = 10;
card2 = 11;
[p1,p2,Name1,Name2]= Playerpoint(card1, card2, n1, n2);
% The function definition
function [p1,p2,Name1,Name2]= Playerpoint(card1, card2, name1, name2)
p1 = card1 + 5;
p2 = card2 + 4;
Name1 = name1;
Name2 = name2;
fprintf("The first user, %s, has %d points.\n", Name1, p1);
fprintf("The second user, %s, has %d points.\n", Name2, p2);
end
% End of m-file.
  1 件のコメント
Shuoze Xu
Shuoze Xu 2022 年 4 月 18 日
Sorry, i am really bust at recent.
Thanks for your help.

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by