此练习的输出? [英] output of this Exercise?
本文介绍了此练习的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,请问您可以逐步将这个问题解答给我7、7、14吗?
非常感谢.
Hi, may u forward me please in steps how this question get to this answer 7, 7 , 14 ?
many thanks.
int cal (ref int x , ref int y , int z)
{
x = x + z + 2;
y = x + z + 3;
z=y;
return x;
}
int x = 2 , y = 4 , z = 1 ;
y = cal ( ref z , ref x , y);
添加了代码块[/编辑]
OP的其他信息是从下面的评论中复制的
老师把答案写成:
A)5、6、4
B)7、7、14
C)14、14、7
D)7,7,7
它需要知道输出中x,y,z的值是什么?
请帮帮我...明天我又要进行C#考试...谢谢
Code block added[/Edit]
OP''s additional information copied from comment below
the teacher put this answers as:
A) 5, 6 , 4
B) 7, 7 , 14
C) 14 , 14 , 7
D) 7 , 7 , 7
it needs to know what''s the value of x , y , z in output?
please please help me ... tomorrow i have again C# exam ... thanks
推荐答案
Y始终等于X,所以只需选择一个
Y is always equal to X, so just pick that one
private static int cal(ref int x, ref int y, int z)
{
Console.WriteLine("Function Start");
Console.WriteLine("ref z= {0};ref x={1};y={2}", x, y, z);
x = x + z + 2;
Console.WriteLine("ref z= {0};ref x={1};y={2}", x, y, z);
y = x + z + 3;
Console.WriteLine("ref z= {0};ref x={1};y={2}", x, y, z);
z = y;
Console.WriteLine("ref z= {0};ref x={1};y={2}", x, y, z);
Console.WriteLine("Function End\n");
return x;
}
private static void Main(string[] args)
{
int x = 2, y = 4, z = 1;
Console.WriteLine("x= {0};y={1};z={2}\n",x,y,z);
y = cal(ref z, ref x, y);
Console.WriteLine("x= {0};y={1};z={2}", x, y, z);
}
x= 2;y=4;z=1
Function Start
ref z= 1;ref x=2;y=4
ref z= 7;ref x=2;y=4
ref z= 7;ref x=14;y=4
ref z= 7;ref x=14;y=14
Function End
x= 14;y=7;z=7
您正在传递一些参数作为参考,这意味着每次在 cal内部分配值时,这意味着什么函数,由于 ref ,该值将自动重定向到外部.所以...第一行的X值不是第二行的X值,因为引用的X改变了外部的值.
另一个非常重要的事情是Richard MacCutchan已经告诉您的内容... 请注意参数的顺序
You are passing some parameters as reference, what it means everytime you assign a value inside the cal function, the value will be automatically redirected to the outside because of ref. So... the value of X at the first line is not the value of X on the second line, because referenced X has changed the value of the outside.
Another very important thing is what Richard MacCutchan already told you... pay attention to the order of the parameters
<br />
outside X = inside Y<br />
outside Y = inside Z<br />
outside Z = inside X<br />
[edit]拼写更正[/edit]
[edit]spelling corrected[/edit]
为什么不使用调试器逐步调试它,您将能够准确地看到发生了什么.还要注意传递给方法cal
的变量的顺序.
Why not step through it with your debugger and you will be able to see exactly what happens. Notice also the order of variables passed in to method cal
.
这篇关于此练习的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文