下面给出的示例代码中的z是多少? Int z,x = 5,y = -10,a = 4,b = 2; z = x ++ - - y * b / a; [英] What number will z in the sample code given below? Int z, x=5, y= -10, a=4, b=2; z=x++ - --y *b/a;

查看:82
本文介绍了下面给出的示例代码中的z是多少? Int z,x = 5,y = -10,a = 4,b = 2; z = x ++ - - y * b / a;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以解决它并解释优先顺序。我没有得到正确的答案。哭| :((我的答案是8.5。但这是错的。我正在努力解决它。请解决它并解释。先谢谢。微笑| :)



< b>我的尝试:



5 ++ - ( - ( - 10))* 2/4

6 - (-11)* 2/4

6 + 11 * 0.5

17 * 0.5

8.5

Can anyone please solve it and explain the precedence. i am not getting right answer. Cry | :(( my answer is 8.5. but it is wrong. i am struggling in it. kindly solve it and explain. Thanks in advance. Smile | :)

What I have tried:

5++ - (--(-10)) * 2/4
6 - (-11)*2/4
6+11*0.5
17*0.5
8.5

推荐答案

引用:

下面给出的示例代码中的z是多少?

What number will z in the sample code given below?

Int z, x=5, y= -10, a=4, b=2; z=x++ - --y *b/a;



制作一个程序并使用调试器查看发生了什么。


Make it a program and use the debugger to see what is going on.

引用:

6 + 11 * 0.5

17 * 0.5

6+11*0.5
17*0.5



您应该了解数学中的运算符优先级。



有一个工具可以让你看到你的代码是什么这样做,它的名字是调试器。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪种期望与现实相符。

当你不明白你的代码在做什么或为什么它做它做的时候,答案就是答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

调试器在这里向您展示您的代码正在做什么,您的任务是与什么进行比较应该这样做。

调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期的效果时,你就接近了一个错误。


You should learn about operator precedence in mathematics.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


试试看看......

如果你看起来很明显发生了什么。

Try it and see...
It's pretty obvious if you look at what is happening.
z = x ++ ...



++发生在价值之后使用x,并且不再引用x,因此为了计算的目的,可以忽略后增量。


++ happens after the value of x is used, and x is not referenced again, so for the purposes of this calculation, the post increment can be ignored.

z = x - --y ...

在使用该值之前发生预先递减,并且再次使用y,因此它相当于

Predecrement occurs before the value is used, and again, y is not used again, so it's the equivalent of

z = x - ( y - 1) ...



现在对于狡猾的位...什么样的值是这些?

如果它们是整数,那么你就不会得到5:你会得到10

如果它们是花车,那么你将得到10.5

为什么会有区别?为什么你不能得到5?

你可以,但你需要使用整数,并强制优先权:


Now for the dodgy bit ... what kind of values are these?
If they are integers, then you will not get 5: you will get 10
If they are floats, then you will get 10.5
Why the difference? Why can't you get 5?
You can, but you need to use integers, and "force" the precedence:

int z = x++ - --y * (b / a);



我会让你理清为什么会这样 - 毕竟这是你的功课......你有足够的线索!


I'll let you sort out why that works - it's your homework, after all ... you have enough clues!


这篇关于下面给出的示例代码中的z是多少? Int z,x = 5,y = -10,a = 4,b = 2; z = x ++ - - y * b / a;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆