2016-09-17 72 views
-1

我在這裏有一個小問題。我想用數字位置替換數組中的所有負數。我的問題是,數組被打印出來,被替換的數字面前,我想打印出數組就被替換後... 這裏是我的代碼:JAVA:使用數字位置替換數組中的數字並打印出來

public class oppgave33{ 

public static void main(String[] args) { 

    int[] heltall = {1, 4, 5, -2, -4, 6, 10, 3, -2}; 
    int counter = 0; 
    int sumNeg = 0; 

    while(counter < heltall.length){ 

    //array print out 

    System.out.println("array[" + counter + "] = " + heltall[counter]); 


    if(heltall[counter] < 0){ 
     System.out.println(heltall[counter]); 

    } 

    //replacing negative numbers 

    if(heltall[counter] < 0){ 
     heltall[counter]=counter; 
    } 
     if(heltall[counter] < 0){ 
     sumNeg++; 
    } 

    //negative numbers position print out 

    if(heltall[counter] < 0){ 
     System.out.println("Negative numbers position in array is : " + counter); 
    } 

    counter++; 
    } 

    //printing out how many negative numbers 

    System.out.println("There are : " + sumNeg + " negative numbers."); 


    } 
    } 

最後一點:如果你刪除在if設置中,負數由陣列中的位置替換,您將獲得打印出負數的位置,以及有多少負數。 我希望你能幫助我! :) 謝謝!

+0

可能重複的http://stackoverflow.com/questions/39528556/replacing-array-values,但這次是從提問者的努力。 :-) –

回答

0

您的代碼中有太多冗餘條件。您的嘗試是非常密切的,你可以簡單地做到以下幾點:

while (counter < heltall.length) { 
    // if the number is negative, replace it with its index 
    if (heltall[counter] < 0) { 
     heltall[counter] = counter; 
    } 
    counter++; 
} 
// outside the loop 
System.out.println(Arrays.toString(heltall)); 

重要的提示:在這種情況下,你應該調試你的代碼。這將幫助您更好地理解代碼流,並發現您不知道的問題。我強烈建議您調試您的當前代碼,然後嘗試修復它。

+0

嗯,但現在當我嘗試在終端編譯有一些錯誤:「System.out.println(Arrays.toString(heltall));」錯誤:無法找到符號「陣列」 – Chris

+0

@Chris您應該導入它:'import java.util.Arrays;' – Maroun

0

你不需要太多的條件來取代負數。當你在循環中得到一個負數時,一個接一個地替換。

刪除冗餘:需要記住的一件事:當您有相同條件的某些語句時,您不需要單獨執行它們。在塊中寫入所有語句。

例如,在你的代碼:

if (heltall[counter] < 0) { 
    System.out.println(heltall[counter]); 
} 

if (heltall[counter] < 0) { 
    heltall[counter] = counter; 
} 

if (heltall[counter] < 0) { 
    sumNeg++; 
} 

if (heltall[counter] < 0) { 
    System.out.println("Negative numbers position in array is : " + counter); 
} 

可以被替代:

if(heltall[counter] < 0) { // do all in the same if condition block 
    System.out.println(heltall[counter]); 
    heltall[counter] = counter; 
    sumNeg++; 
    System.out.println("Negative numbers position in array is : " + counter); 
} 

解決方案:總之,整個代碼看起來是這樣的:

while (counter < heltall.length) { 
    // replacing negative numbers 
    if (heltall[counter] < 0) { 
     heltall[counter] = counter; 
     sumNeg++; 
    } 
    counter++; 
} 
System.out.println("There were : " + sumNeg + " negative numbers."); 
System.out.println("Array after replacing negative numbers: "+Arrays.toString(heltall)); 
0

以下是您的代碼的工作版本:

注意:您必須在更換後放置打印命令。代碼按照順序逐步執行。頂部行的語句首先運行,然後是較低的行(確定按順序)。

public class oppgave33{ 

public static void main(String[] args) { 

    int[] heltall = {1, 4, 5, -2, -4, 6, 10, 3, -2}; 
    int counter = 0; 
    int sumNeg = 0; 

while(counter < heltall.length){ 

    if(heltall[counter] < 0){ 

     //replacing negative numbers 
     heltall[counter]=counter; 

     //counting negative number amount 
     sumNeg++; 

     //array print out after replace 
     System.out.println("array[" + counter + "] = " + heltall[counter]); 

     //negative numbers position print out 
     System.out.println("Negative numbers position in array is : " + counter); 
    } 



    counter++; 
    } 

    //printing out how many negative numbers 

    System.out.println("There are : " + sumNeg + " negative numbers."); 


    } 
    } 
相關問題