3

配列内のすべての値に 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.
4

6 に答える 6

5

あなたの例では、子配列を乗算していますが、新しい配列を返しています。新しい配列に子配列を掛ける必要があります。

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 行目で割り当てられますが、この時点では配列の各インデックスが初期化されていないため、そこにどのような値が含まれているかはわかりません。

于 2013-10-26T15:52:50.260 に答える
2

メソッドで新しい配列を作成する必要はありません (また、変更なしで古い配列を返します)。だからただする

public static int[] multiply(int[] children, int number) {
    for(int i = 0; i < children.length; i++) {
        children[i] = children[i] * number;
    }
    return children;
}
于 2013-10-26T15:44:27.490 に答える
1

変更する必要があります

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

 array[i] = children[i] * number;
于 2013-10-26T15:44:20.193 に答える
1

あなたの質問を正しく理解している場合:

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

に変更する必要があります

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

あなたが戻ってきていることを考えるとarray、ではありませんchildren

于 2013-10-26T15:44:38.270 に答える
0

2 番目の for ループでは、次のようになります。

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

また、 のすべての値children[i]が より劣っていることを確認してください((2^31 - 1)/number) +1

于 2013-10-26T15:44:17.910 に答える