C#math.max没有返回结果。 [英] C# math.max not returning results.

查看:117
本文介绍了C#math.max没有返回结果。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取2列之间的Max值并将其返回到第三列,如果它们是相同的返回值。



调试中的每一行显示一个值。除了d3之外,它应该在确定最高值之后放置。



关于我做错了什么的想法?



德格的图像

图像 - TinyPic - 免费图像托管,照片共享&视频托管 [ ^ ]

$ b



 decimal d1 = decimal.Parse(string.IsNullOrEmpty(rows.Cells [41]。 Value.ToString())?0:rows.Cells [41] .Value.ToString()); 
decimal d2 = decimal.Parse(string.IsNullOrEmpty(rows.Cells [46] .Value.ToString())?0:rows.Cells [46] .Value.ToString());
decimal d3 = decimal.Parse(string.IsNullOrEmpty(rows.Cells [37] .Value.ToString())?0:rows.Cells [37] .Value.ToString());
decimal AL = Math.Max(d1,d2);
AL = Math.Max(d3,AL);





我尝试过:



上面的代码应该工作,返回列是空白的开始。

解决方案

你不要告诉我们你对最终值的处理方式(在 AL 中),但由于它只是一个变量,除非你将它添加到一列,否则它不会显示。 />


你需要做的第一件事就是打破调试器,仔细查看用你的数据运行代码时到底发生了什么:在行上放一个断点< pre lang =c#> AL = Math.Max(d3,AL);

并查看 d1 中的内容, d2 d3 AL

那个应该给你一个关于发生了什么的线索,一旦你有了相关的数据/代码,你就可以仔细查看相关的数据/代码。



对不起 - 但是我们做不到任何适合你的!


引用:

我想获取2列之间的Max值并将其返回到第三列,如果它们返回的值相同。



此代码计算最多3个值,不要尝试将结果存储在另一列中。



您的代码不要按照你期望的方式行事,或者你不明白为什么!



有一个几乎通用的解决方案:一步一步地在调试器上运行你的代码,检查变量。

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

调试器中没有魔法,它不知道你的代码应该做什么,它没有发现错误,它只是通过向你展示发生了什么来帮助你。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。



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


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

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

在Visual Studio中调试C#代码 - YouTube [< a href =https://www.youtube.com/watch?v=u-HdLtqEOogtarget =_ blanktitle =New Window> ^ ]



这里的调试器只显示你的代码正在做什么,你的任务是与它应该做什么进行比较。


I want to get the Max value between 2 columns and return it to a third column, if they are the same return that values.

Each line in debug shows a value. except for d3 where the value should be placed after it determines the highest value.

Ideas of what I'm doing wrong?

Image of Degug
Image - TinyPic - Free Image Hosting, Photo Sharing & Video Hosting[^]



decimal d1 = decimal.Parse(string.IsNullOrEmpty(rows.Cells[41].Value.ToString()) ? "0" : rows.Cells[41].Value.ToString());
                decimal d2 = decimal.Parse(string.IsNullOrEmpty(rows.Cells[46].Value.ToString()) ? "0" : rows.Cells[46].Value.ToString());
                decimal d3 = decimal.Parse(string.IsNullOrEmpty(rows.Cells[37].Value.ToString()) ? "0" : rows.Cells[37].Value.ToString());
                decimal AL = Math.Max(d1, d2);
                AL = Math.Max(d3,AL);



What I have tried:

the above code should work the return column is blank st start with.

解决方案

You don't show us what you do with the eventual value (in AL) but since it's just a variable unless you "add it to a column" it's not going to show.

The first thing you need to do is break out the debugger and look closely at exactly what is happening when you run that code with your data: put a breakpoint on the line

AL = Math.Max(d3,AL);

and look at what is in d1, d2, d3, and AL
That should give you a clue as to what is happening and you can look closer at the relevant data / code once you have that.

Sorry - but we can't do any of that for you!


Quote:

I want to get the Max value between 2 columns and return it to a third column, if they are the same return that values.


This code calc the max of 3 values and don't try to store the result in another column.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
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 know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
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[^]
Debugging C# Code in Visual Studio - YouTube[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


这篇关于C#math.max没有返回结果。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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