[英]How to replace an array containing number 3 with 4 in java?
void replace3sWith4s(int[] replace){
for (int i = 0;i<replace.length;i++){
if (replace[i]==3);{
replace[i]=4;
}
}
}
My program is replacing all numbers with #4 but I want to have an array that contains 3, takes an integer array, and changes any element that has the value 3 to instead have the value 4. 我的程序将所有数字替换为#4,但是我想拥有一个包含3的数组,采用一个整数数组,并将任何具有值3的元素更改为具有值4。
if (replace[i]==3);
^^^
Remove the semicolon. 删除分号。 It should be
它应该是
if (replace[i]==3) {
replace[i]=4;
}
The semicolon changes the meaning to 分号将含义更改为
if (replace[i]==3)
;//do nothing
// Separate New block
{
replace[i]=4;
}
(replace[i]==3);
Is like writing 就像写作
(replace[i]==3) { }
Which does nothing. 什么都不做。
Your code is equivalent to this code: 您的代码等同于以下代码:
void replace3sWith4s(int[] replace){
for (int i = 0;i<replace.length;i++){
if (replace[i]==3) { }
replace[i]=4; //Always reachable
}
}
}
To fix your code, remove the semicolon: 要修复您的代码,请删除分号:
(replace[i]==3);
^
also You can try this method at if statement according to your array type 您也可以根据您的数组类型在if语句中尝试此方法
if (replace[i].equals("3")){
replace[i]=4;
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.