首页 > 解决方案 > 为第一个空的 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 的值

标签: javaandroid-studio

解决方案


您可以使用 ArrayList 并添加用户输入,直到列表具有特定长度,然后您进行计算


推荐阅读