假设您有 70 个独立的 int 变量,并且您想要分析所有这些变量并将任何一个等于 0 的变量设置为 1 或其他数字。
您可以只写一个,而不是为每个变量编写 70 个 if else 语句吗?
有没有办法做这样的事情:
if("anyInt"==0){
"thatInt" = 1;}
?
(应该提到我不知道什么是“集合”)
最佳答案
使用 int[]
然后使用 for 循环迭代它们
int[] nums = new int[70];
//put all your numbers in the array, i.e. nums[0] = 5;
for(int i = 0; i < nums.length; i++){
if(nums[i] == 0){
//Do whatever to the number
}
}
关于java - 分析/过滤所有变量而不声明每个变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15538349/