2013-10-26 42 views
2

我需要將數組中的所有值複製到3000,這反過來會創建一個新的數組,我將用它來從另一個數組中減去。我試圖創建一個單獨的方法,可以爲我做到這一點,但是我在乘法數組中返回的所有數字和符號都是奇怪的?將數組中的所有元素乘以外部數字?

這裏是我寫的

public static void main(String[] args) 
{  
    int numberOfTaxpayers = Integer.parseInt(JOptionPane.showInputDialog("Enter how many users you would like to calculate taxes for: "); 
    int[] usernumChild = new int[numberOfTaxPayers]; 
    for (int i = 0; i < usernumChild.length; i++) 
    { 
     usernumChild[i] = Integer.parseInt(JOptionPane.showInputDialog("Enter number of children for user "+ (i+1) +": ")); 
    }//this for loop finds out the number of children per user so we can later multiply each input by 3000 to create an array that determine dependency exemption for each user 
int[] depndExemp = multiply(usernumChild, 3000);//this was the calling of the multiply method... somewhere here is the error!! 
}//end main method 
public static int[] multiply(int[] children, int number) 
{ 
    int array[] = new int[children.length]; 
    for(int i = 0; i < children.length; i++) 
    { 
     children[i] = children[i] * number; 
    }//end for 
    return array; 
}//this is the method that I was shown in a previous post on how to create return an array in this the dependency exemption array but when I tested this by printing out the dependency array all I received were a jumble of wrong numbers. 
+0

你說的「一個新的數組,我將使用來自另一個陣列減去」是什麼意思? – 2013-10-26 15:43:48

+0

@Lutz Horn我還沒有包括那部分,但我需要做的是將依賴性數組(每個輸入乘以3000)從另一個包含每個用戶總收入的數組中減去導致另一個陣列在淨收入下創建...由於某種原因,我們的教師想要爲每種數據類型提供一個陣列 – prodo

回答

4

在你的例子中,你正在乘你的孩子數組,但返回你的新數組。您需要將您的新數組與您的子數組相乘。

1 public static int[] multiply(int[] children, int number) 
2 { 
3  int array[] = new int[children.length]; 
4  for(int i = 0; i < children.length; i++) 
5  { 
6   array[i] = children[i] * number; 
7  }//end for 
8  return array; 
9 } 

你得到奇怪的符號,是因爲你正在返回未初始化值。數組本身在第3行被分配,但此時數組中的每個索引都沒有被初始化,所以我們並不真正知道那裏有什麼值。

+0

我會在哪裏初始化返回值?我最初是否會改變第二種方法? – prodo

+0

不明白你的答案 – Tyler

+0

好吧我現在明白了,謝謝,但我怎樣才能得到usernumChild的內容到另一種方法?我認爲用multiply(usernumChild,3000)聲明它會這樣做?那麼我可能只是擺脫第二種方法,並在主體中執行算法,所以我不必傳遞數組呢?簡單是目前的關鍵。 – prodo

0

在你的第二個for循環的代碼應該是:

for(int i = 0; i < children.length; i++){ 
     array[i] = children[i] * number; 
}//end for 

同時確保的children[i]所有值都劣比((2^31 - 1)/number) +1

+0

嗯我試過了,仍然得到system.print [I @ 4a3a6e5c – prodo

+0

@prodo這意味着,重新打印您創建的int數組的引用。爲了打印你的數組,你可以創建一個for循環並打印每個值或使用'System.out.println(Arrays.toString(myArray));' –

+0

我將不得不去創建一個循環的路線,所以你建議創建一個擴展的循環?將depndExemp的值放入for(int reprint:depndExemp){System.out.println(reprint)} – prodo

1

您需要更改

children[i] = children[i] * number; 

array[i] = children[i] * number; 
1

你真的沒有在你的方法來創建新的數組(和你也返回舊的沒有任何變化)。所以只是做

public static int[] multiply(int[] children, int number) { 
    for(int i = 0; i < children.length; i++) { 
     children[i] = children[i] * number; 
    } 
    return children; 
} 
+0

我可以在不創建新方法的情況下進行乘法運算嗎?我想盡量讓這個程序儘可能乾淨和簡單。此時還剩下36個小時的時間來完成這個任務,並且需要更多的時間來完成最終結果,至少可以說是黯淡! – prodo

+0

是的,你可以,但最好有多種方法,然後是一個巨大的方法,所以這是一個正確的設計。經驗法則是每種方法都應該適合屏幕 –

1

如果我正確理解你的問題:

children[i] = children[i] * number; 

應改爲

array[i] = children[i] * number; 

考慮到你正在返回array,不children

3

使用Java 8流也可以是簡單:

public static int[] multiply(int[] children, int number) { 

    return Arrays.stream(children).map(i -> i*number).toArray(); 

} 
相關問題