私のコードで、平均値を上回っている配列内の値が正しく印刷されないのを解明しようとしています。私は同様の問題を持つ別のユーザーのstackoverflowの例を見ましたが、それでも意味をなさない。私は平均以上の値の現在の出力を理解していません。平均数を超える配列内の数字を見つける
すべての配列値の計算平均を上回る値を配列に出力するにはどうすればよいですか?
私は助けていただきありがとうございます。
マイコード:
public class Inequality
{
public static void main(String[] args)
{
// list of incomes in thousands
int[] income = {2,10, 532, 4, 53, 28, 291, 38, 6, 17, 73, 21};
int sum = 0;
int average;
int aboveAverage = 0;
for (int i = 0;i< income.length;i++)
{
sum = sum + income[i];
}
average = sum/income.length;
System.out.println("Average of income array: " + average);
for (int i = 0; i < income.length; i++)
{
if (income[i]>average)
{
aboveAverage++;
}
}
System.out.println("There are " + aboveAverage + " numbers above the average.");
System.out.println("Range of numbers above the average: " + income);
}
}
マイ出力:
Average of income array: 89
There are 2 numbers above the average.
Range of numbers above the average: [[email protected]
アレイオブジェクト全体を印刷しています。あなたは「収入」に平均以上の数字が含まれると思いますか? –
上記の平均値を計算するのと同様の方法で、ループ、 'if'ステートメント、' print'ステートメントを使って出力します。あるいは、すでに持っているループに 'print'ステートメントを追加するだけです。 – Andreas