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)
{
if (userinput <= pagetotal)
{
_data = Enumerable.Repeat(pagetotal / userinput, userinput - 1).ToList();
int y = (pagetotal - pagetotal / userinput * (userinput - 1));
_data.Add(y);
}
}
return _data;
}
解决方案
_data
null
仅当所有这些条件都满足时才设置为非值true
:
userinput <= 10
-这是true
,因为userinput
是10
userinput != 0
-这是true
,因为userinput
是10
userinput <= pagetotal
- 这是false
,因为userinput
是10
,pagetotal
而是9
您需要决定在pagetotal
小于时返回什么userinput
。目前它是null
,但您可能会返回一个空列表:
if (userinput <= pagetotal) {
...
} else {
_data = ...
}
推荐阅读
- discord - 尝试获取我的机器人所在行会的邀请时出错
- python - 如何确定感叹号是否在字符串的末尾?
- python - 我想将 torch nn.Conv2D 更改为 tensorflow 2.0 Conv2D
- certificate - Monaca 证书问题
- configuration - 在 CLion 中,什么是正确的运行配置?我无法理解我应该为“可执行文件”添加什么
- javascript - 如何在javascript react native库中访问python库
- c++ - C++/C在不同用户权限下在程序内执行代码(程序最初是用sudo调用的)
- python - 函数为某些数组返回错误值
- docker - 如何处理高山 docker 镜像中的缺失?
- c# - C#通过bash创建符号链接什么都不做