给第一个空整数赋值

huangapple go评论71阅读模式
英文:

Assigning a value to the first empty int

问题

我有一个简单的接口,用户输入一个数字(可以是正数、负数或0)。

我有20个int变量,命名为score1-20。

在按下按钮后,我希望循环遍历每个变量,直到找到下一个空变量,然后将用户输入的数字赋值给它。如果所有变量都有值,那么我将进行一个计算。

int score1.....score20;

calculateBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        //将输入值转换为整数的代码

        if(score1 == null)
            score1 = currentScore;

        //使用else-if语句遍历所有20个分数,直到找到一个不为空的。如果填充了score20,则进行另一个计算以得出最终输出。

    }
});

我知道可能有一种更好的方法可以使用for循环来遍历它们。

是否有一种更好的方法来检查int是否为空,而不是将数据类型更改为可为空的Integer?我不想将所有的int都分配为0,因为int可能携带值0。

英文:

I have a simple interface which the user inputs a number into (positive or negative or 0)

I have 20 int variables score1-20

upon pressing a button I then want to cycle through each variable until I find the next empty one and then assign the number the user input. If all variables have values then I will do a calculation.


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.

             }
            }
        });

I know there is probably a better way to cycle through them using a for loop.

Is there a better way to check if the int is empty rather than changing the data type to an Integer which can be null? I don't want to assign all the ints as 0 as the int could potentially hold a value of 0

答案1

得分: 1

你可以使用ArrayList,在列表中添加用户输入,直到列表达到特定的长度,然后进行计算。

英文:

You could use an ArrayList and add the user input until the list has a specific length and then you do your calculation

huangapple
  • 本文由 发表于 2020年9月1日 20:38:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/63687856.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定