C# 求最大数

发布于 2024-10-18 23:46:04 字数 85 浏览 3 评论 0原文

这是我第一次使用c#,所以我不太熟悉它。如果我让用户输入 3 个数字,我想创建一个简单的程序来查找最大的数字。我只需要知道在代码中放入什么,因为我不太确定。

It's the first time I am using c# so I am not very familiar with it. I would like to create a simple program to find the biggest number if I have the user entering 3 numbers. I just need to know what to put in the code, because I am not very sure.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

迟月 2024-10-25 23:46:04

使用Math.Max:

int x = 3, y = 4, z = 5;
Console.WriteLine(Math.Max(Math.Max(x, y), z));

Use Math.Max:

int x = 3, y = 4, z = 5;
Console.WriteLine(Math.Max(Math.Max(x, y), z));
把梦留给海 2024-10-25 23:46:04

有 Linq Max() 扩展方法。它适用于所有常见的数字类型(int、double、...)。由于它适用于任何实现 IEnumerable 的类,因此它适用于所有常见容器,例如数组 T[]List >,...

要使用它,您需要在 C# 文件的开头添加 using System.Linq ,并且需要引用 System.Core 程序集。两者均在新项目(C# 3 或更高版本)上默认完成。

int[] numbers=new int[]{1,3,2};
int maximumNumber=numbers.Max();

您还可以使用仅适用于两个数字的 Math.Max(a,b)。或者自己写一个方法。这也不难。

There is the Linq Max() extension method. It's available for all common number types(int, double, ...). And since it works on any class that implements IEnumerable<T> it works on all common containers such as arrays T[], List<T>,...

To use it you need to have using System.Linq in the beginning of your C# file, and need to reference the System.Core assembly. Both are done by default on new projects(C# 3 or later)

int[] numbers=new int[]{1,3,2};
int maximumNumber=numbers.Max();

You can also use Math.Max(a,b) which works only on two numbers. Or write a method yourself. That's not hard either.

眼眸印温柔 2024-10-25 23:46:04

您可以使用 Math.Max 方法返回两个数字的最大值,例如对于int

intMaximum = Math.Max(number1, Math.Max(number2, number3))

还有Max() 方法来自 LINQ,您可以在任何 IEnumerable 上使用它。

You can use the Math.Max method to return the maximum of two numbers, e.g. for int:

int maximum = Math.Max(number1, Math.Max(number2, number3))

There ist also the Max() method from LINQ which you can use on any IEnumerable.

待"谢繁草 2024-10-25 23:46:04
using System;
using System.Linq;

class Program
{
    static void Main(string[] args)
    {
        int[] numbers = { 3, 9, 5 };
        int biggestNumber = numbers.Max();
        Console.WriteLine(biggestNumber);
        Console.ReadLine();
    }
}
using System;
using System.Linq;

class Program
{
    static void Main(string[] args)
    {
        int[] numbers = { 3, 9, 5 };
        int biggestNumber = numbers.Max();
        Console.WriteLine(biggestNumber);
        Console.ReadLine();
    }
}
玩物 2024-10-25 23:46:04

我也需要找到一种方法来做到这一点,使用来自不同地方的数字而不是集合中的数字。我确信在 C# 中有一种方法可以做到这一点...尽管从它的外观来看,我正在混淆我的语言...

无论如何,我最终编写了几个通用方法来做到这一点

    static T Max<T>(params T[] numberItems)
    {
        return numberItems.Max();
    }

    static T Min<T>(params T[] numberItems)
    {
        return numberItems.Min();
    }

......这样称呼他们...

    int intTest = Max(1, 2, 3, 4);
    float floatTest = Min(0f, 255.3f, 12f, -1.2f);

I needed to find a way to do this too, using numbers from different places and not in a collection. I was sure there was a method to do this in c#...though by the looks of it I'm muddling my languages...

Anyway, I ended up writing a couple of generic methods to do it...

    static T Max<T>(params T[] numberItems)
    {
        return numberItems.Max();
    }

    static T Min<T>(params T[] numberItems)
    {
        return numberItems.Min();
    }

...call them this way...

    int intTest = Max(1, 2, 3, 4);
    float floatTest = Min(0f, 255.3f, 12f, -1.2f);
罗罗贝儿 2024-10-25 23:46:04

如果你的数字是 a、b 和 c,那么:

    int a = 1;
    int b = 2;
    int c = 3;

    int d =  a > b ? a : b;
    return c > d ? c : d;

这可能会变成“我们可以用多少种不同的方法来做到这一点”类型的问题之一!

If your numbers are a, b and c then:

    int a = 1;
    int b = 2;
    int c = 3;

    int d =  a > b ? a : b;
    return c > d ? c : d;

This could turn into one of those "how many different ways can we do this" type questions!

蓦然回首 2024-10-25 23:46:04

这是查找最大/最大数字的简单逻辑

输入:11, 33, 1111, 4, 0 输出:1111

namespace PurushLogics
{
    class Purush_BiggestNumber
    {
        static void Main()
        {
            int count = 0;
            Console.WriteLine("Enter Total Number of Integers\n");
            count = int.Parse(Console.ReadLine());

            int[] numbers = new int[count];

            Console.WriteLine("Enter the numbers"); // Input 44, 55, 111, 2 Output = "111"
            for (int temp = 0; temp < count; temp++)
            {
                numbers[temp] = int.Parse(Console.ReadLine());
            }

            int largest = numbers[0];
            for (int big = 1; big < numbers.Length; big++)
            {
                if (largest < numbers[big])
                {
                    largest = numbers[big];
                }
            }
            Console.WriteLine(largest);
            Console.ReadKey();
        }
    }
}

Here is the simple logic to find Biggest/Largest Number

Input : 11, 33, 1111, 4, 0 Output : 1111

namespace PurushLogics
{
    class Purush_BiggestNumber
    {
        static void Main()
        {
            int count = 0;
            Console.WriteLine("Enter Total Number of Integers\n");
            count = int.Parse(Console.ReadLine());

            int[] numbers = new int[count];

            Console.WriteLine("Enter the numbers"); // Input 44, 55, 111, 2 Output = "111"
            for (int temp = 0; temp < count; temp++)
            {
                numbers[temp] = int.Parse(Console.ReadLine());
            }

            int largest = numbers[0];
            for (int big = 1; big < numbers.Length; big++)
            {
                if (largest < numbers[big])
                {
                    largest = numbers[big];
                }
            }
            Console.WriteLine(largest);
            Console.ReadKey();
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文