确定具有最大值的变量的名称 [英] Determine the name of a variable with the maximum value

查看:98
本文介绍了确定具有最大值的变量的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在变量vpk中存储了3个值.我想在其中找到最大的.变量的值为:v = 3p = 6k = 2.

I have 3 values stored in the variables v, p, and k. I want to find the greatest among them. The values of the variables are: v = 3, p = 6, and k = 2.

结果应为包含最大值的变量的名称-文本vpk.

The result should be the name of the variable that contains the maximum value—the text v, p, or k.

我该怎么做?

推荐答案

好吧,您只是避免了已经发布的每个答案,而没有在第一时间就清楚地说明您想要什么.您可以轻松地扩展 Enumerable.Max 的使用为您提供您现在想要的东西:

Well, you just obviated every answer already posted by not clearly stating what you wanted the first time. You can extend the use of Enumerable.Max easily to give you what you now want:

string max = 
     new[] {
         Tuple.Create(v, "v"),
         Tuple.Create(p, "p"),
         Tuple.Create(k, "k")
     }.Max()
      .Item2;

一种替代方法是:

string max = v >= p && v >= k ? "v" : p >= v && p >= k ? "p" : "k";

但是您看到它比第一个版本具有更少的可读性吗?

But do you see how much less readable that is than the first version?

这篇关于确定具有最大值的变量的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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