[英]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;
}
}
}
我的程序將所有數字替換為#4,但是我想擁有一個包含3的數組,采用一個整數數組,並將任何具有值3的元素更改為具有值4。
if (replace[i]==3);
^^^
刪除分號。 它應該是
if (replace[i]==3) {
replace[i]=4;
}
分號將含義更改為
if (replace[i]==3)
;//do nothing
// Separate New block
{
replace[i]=4;
}
(replace[i]==3);
就像寫作
(replace[i]==3) { }
什么都不做。
您的代碼等同於以下代碼:
void replace3sWith4s(int[] replace){
for (int i = 0;i<replace.length;i++){
if (replace[i]==3) { }
replace[i]=4; //Always reachable
}
}
}
要修復您的代碼,請刪除分號:
(replace[i]==3);
^
您也可以根據您的數組類型在if語句中嘗試此方法
if (replace[i].equals("3")){
replace[i]=4;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.