Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.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#:列表的抛出异常值不能为null<;int>;_C# - Fatal编程技术网

C#:列表的抛出异常值不能为null<;int>;

C#:列表的抛出异常值不能为null<;int>;,c#,C#,当用户输入为10时,测试函数返回null。请指导我如何处理这种情况 List<int?> test10 = testInt(9, 10).ToList(); public static List<int?> testInt(int pagetotal, int userinput) { List<int?> _data = null; if (userinput <= 10 && userinput != 0)

当用户输入为10时,测试函数返回null。请指导我如何处理这种情况

List<int?> test10 = testInt(9, 10).ToList();

public static List<int?> testInt(int pagetotal, int userinput)
{
    List<int?> _data = null;

    if (userinput <= 10 && userinput != 0)
    {
        if (userinput <= pagetotal)
        {
            _data = Enumerable.Repeat(pagetotal / userinput, userinput - 1).ToList();
            int y = (pagetotal - pagetotal / userinput * (userinput - 1));
            _data.Add(y);

        }
    }

    return _data;
}
List test10=testInt(9,10).ToList();
公共静态列表测试(int pagetotal,int userinput)
{
列表_data=null;

如果(userinput
\u数据
仅在所有这些条件均为
真时设置为非
值:

  • userinput
    userinput=10(页面总数=9)。
    我认为最好先规范化用户输入。
    您可以添加以下行:

    if(userInput > total)
        userInput = total;
    if(userInput <1)
        userInput =1;
    
    if(用户输入>总计)
    用户输入=总数;
    
    if(对于初学者,用户输入,如果您已经返回一个
    列表
    ,则无需对其调用
    ToList
    )其次,如果您的
    userinput
    值是11+或0,或者它是
    pagetotal
    ,您将返回
    null
    。我很确定这会导致您的
    null
    问题。在您的示例中,您使用
    pagetotal=9
    userinput=10
    调用它,这将导致基于您的if语句的null返回s
    userinput <= pagetotal
    
    if(userInput > total)
        userInput = total;
    if(userInput <1)
        userInput =1;