### 程序計算五個數字的平均值

#### [英]Program to calculate average of five numbers

I am a beginner in java and wrote a program to calculate the average of five numbers. But i don't know why it came up with an error. So what can I do to resolve this.

``````public static void main(String args[]){
int nums[]=new int[5];
nums[]={1,3,5,7,9};
int num2=0,i;
for(i=0;i<=4;i++){
num2=nums[i]+num2;
}
System.out.println(num2/5);
}
``````

Thanks

## 1 个解决方案

### #1

3

You can't assign a value to an array using initializer notation you after have declared it. (The hint is in the name - you can only use it to initialize the array).

``````int nums[]=new int[5];
nums[]={1,3,5,7,9};
``````

use

``````int nums[] = {1,3,5,7,9};
``````

You can use:

``````nums = new int[] {1,3,5,7,9};
``````

but you don't need to assign a value to the array first using `int nums[] = new int[5];` - `int nums[];` is sufficient.

Also, it is generally considered better to use `int[] nums` instead of `int nums[]`: the "array-ness" is part of the type, not the variable name, so keep the `[]` with the `int`.

(The `int nums[]` form was included in early versions of Java to make the language more familiar to C/C++ programmers, and we're now stuck with it.)

(int nums []表單包含在Java的早期版本中,以使C / C ++程序員更熟悉該語言,現在我們已經堅持使用它了。)