java - 为第一个空的 int 赋值
问题描述
我有一个简单的界面,用户输入一个数字(正数或负数或 0)
我有 20 个 int 变量 score1-20
按下按钮后,我想循环遍历每个变量,直到找到下一个空变量,然后分配用户输入的数字。如果所有变量都有值,那么我将进行计算。
int score1.....score20;
calculateBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//code to convert an input value to an int
if(score1 == null)
score1 = currentScore;
//else-if statements to go through all 20 scores until one is not null. If you
populate score20 then do another calculation to give the final output.
}
}
});
我知道可能有更好的方法可以使用 for 循环来循环它们。
有没有更好的方法来检查 int 是否为空,而不是将数据类型更改为可以为 null 的 Integer?我不想将所有 int 分配为 0,因为 int 可能持有 0 的值
解决方案
您可以使用 ArrayList 并添加用户输入,直到列表具有特定长度,然后您进行计算
推荐阅读
- nginx - Nginx:404 Not Found错误作为反向代理
- flutter - 未处理的异常:MissingPluginException(在通道 flutter.baseflow.com/permissions/methods 上找不到方法 requestPermissions 的实现)
- docker - Nginx 作为多个 Tomcat Docker 容器的反向代理
- tensorflow - (tensorflow 2.4.1)如何得到一个参差不齐的张量具有确定的最后一维形状?
- c# - 具有具体类覆盖的通用扩展方法
- mysql - MySQL 错误代码:3948 加载本地数据已禁用;这必须在客户端和服务器端都启用
- python-3.x - 调用 line_profiler 时没有名为“line_profiler._line_profiler”的模块
- mysql - sql查看日期出勤和请假
- visual-studio-code - VSCode 在 Deno 中显示 ES6 导入错误
- python - TimeoutError: [WinError 10060] 连接尝试失败,因为连接方在一段时间后没有正确响应 Email-Listener