Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 查找不带数组的第二大数_C#_Algorithm - Fatal编程技术网

C# 查找不带数组的第二大数

C# 查找不带数组的第二大数,c#,algorithm,C#,Algorithm,我必须制作一个程序,从用户输入中返回第二高的数字 用户可以输入最小值的2个数字和最大值的10个数字。用户只能输入整数(不是小数),以停止我正在使用的程序0 我的问题是:如果我输入1,2,2,0它输出2,而不正确,它应该输出1 以下是我目前正在做的事情: static void checking(double n, ref double max, ref double smax) { if (n > max) { smax = max; max

我必须制作一个程序,从用户输入中返回第二高的数字

用户可以输入最小值2个数字和最大值10个数字。用户只能输入整数(不是小数),以停止我正在使用的程序0

我的问题是:如果我输入1,2,2,0它输出2,而不正确,它应该输出1

以下是我目前正在做的事情:

static void checking(double n, ref double max, ref double smax)
{
    if (n > max)
    {
        smax = max;
        max = n;
    }
    else if (n > smax)
    {
        smax = n;
    }
}
static void Main(string[] args)
{
    double n = 1, max = -99999999, smax = -99999999, ISsmaxrepeating = 0;
    int i = 0;

    while (n != 0 && i < 10)
    {
        Console.WriteLine("Input number");
        n = double.Parse(Console.ReadLine());
        if (n % 1 == 0)
        {
            checking(n, ref max, ref smax);
            i++;
        }
        else
        {
            smax =0;
            break;
        }
    }
    if (smax != 0) 
    {
        Console.WriteLine("secondmax is {0}", smax);
    }
    else
    {
        Console.WriteLine("error");
    }

    Console.ReadLine();
}
静态无效检查(双n、双最大参考值、双最大参考值)
{
如果(n>最大值)
{
smax=最大值;
max=n;
}
否则如果(n>smax)
{
smax=n;
}
}
静态void Main(字符串[]参数)
{
双n=1,最大值=-9999999,smax=-9999999,ISsmaxrepeating=0;
int i=0;
而(n!=0&&i<10)
{
控制台写入线(“输入号码”);
n=double.Parse(Console.ReadLine());
如果(n%1==0)
{
检查(n,参考最大值,参考最大值);
i++;
}
其他的
{
smax=0;
打破
}
}
如果(smax!=0)
{
WriteLine(“secondmax是{0}”,smax);
}
其他的
{
控制台写入线(“错误”);
}
Console.ReadLine();
}
下面是一些测试用例:

例1:

输入:105-4850

输出:8

例2:

输入:50

输出:“错误”

例3:

输入:10

输出:“错误”

例4:

输入:12345678910

输出:9


如果您已经有一个数字,请忽略它:

...
while (n != 0 && i < 10)
{
    Console.WriteLine("Input number");
    n = double.Parse(Console.ReadLine());
    if (n % 1 == 0)
    {
        if(n != max && n != smax)
            checking(n, ref max, ref smax);

        i++;
    }
    else
    {
        smax =0;
        break;
    }
}
...
。。。
而(n!=0&&i<10)
{
控制台写入线(“输入号码”);
n=double.Parse(Console.ReadLine());
如果(n%1==0)
{
如果(n!=max&&n!=smax)
检查(n,参考最大值,参考最大值);
i++;
}
其他的
{
smax=0;
打破
}
}
...

顺便说一下,我不相信您处理错误案例的方式。

首先,检查方法的完整代码:

private static void checking(double n, ref double max, ref double smax)
    {
        if (n > max)
        {
            smax = max;
            max = n;
        }
        // Extra check added here
        else if (n > smax && n != max)
        {
            smax = n;
        }
    }
我用几个输入(5,5,5,0和1,2,1,0)测试了它,它的行为与预期的一样(“1”在第二种情况下,“error”在第一种情况下)

而且,您的
n%1==0
检查是一件非常奇怪的事情;我建议你要么修改它,要么添加一条评论,解释你为什么这么做是为了读者的利益。

最终计划:)

静态无效检查(双n、双最大参考值、双最大参考值)
{
如果(n>最大值)
{
smax=最大值;
max=n;
}
否则如果(n>smax)
{
smax=n;
}
}
静态void Main(字符串[]参数)
{
double n=1,max=double.MinValue,smax=double.MinValue;
int i=0,stopInput=0;
而(n!=0&&i<10)
{
控制台写入线(“输入号码”);
n=double.Parse(Console.ReadLine());
if(n%1==0&&n!=0)//此(n%1==0)部分检查数字是否为十进制
{
如果(n!=max&&n!=smax)
检查(n,参考最大值,参考最大值);
i++;
}
其他的
{
打破
}
}
if(stopInput==1&&smax!=Double.MinValue)
{
WriteLine(“secondmax是{0}”,smax);
}
其他的
{
控制台写入线(“错误”);
}
Console.ReadLine();
}

不使用数组。。。这有点令人困惑。它与数组有什么关系?你的意思是不能在任何地方存储数字列表吗?如果只使用变量:)那么你必须将其作为递归函数调用…n%1==0对于整数来说是非常正确的。这个//如果(n!=max&&n!=smax)//不起作用:)我有if(n%1==0)用于检查用户输入的数字是否不是十进制:)我的意思是作为附加条件。不是作为替代。我添加了更多的上下文。也许,它现在变得更清晰了。现在我明白它是如何工作的:D,但伙计们,我还有一个问题。。。我不知道如何处理负数我应该只分配给max和smax最小的双精度值吗?是的,这是
double.NegativeInfinity
static void checking(double n, ref double max, ref double smax)
    {
        if (n > max)
        {
            smax = max;
            max = n;
        }
        else if (n > smax)
        {
            smax = n;
        }
    }
    static void Main(string[] args)
    {
        double n = 1, max = Double.MinValue, smax = Double.MinValue;
        int i = 0, stopInput = 0;

        while (n != 0 && i < 10)
        {
            Console.WriteLine("Input number");
            n = double.Parse(Console.ReadLine());
            if (n % 1 == 0 && n !=0) //this (n % 1 == 0) part checks if number is not decimal
            {
                if (n != max && n != smax)
                    checking(n, ref max, ref smax);
                i++;
            }
            else
            {
                break;
            }

        }
        if (stopInput ==1 && smax != Double.MinValue) 
        {

            Console.WriteLine("secondmax is {0}", smax);

        }
        else
        {
            Console.WriteLine("error");
        }

        Console.ReadLine();


    }