我正在尝试让用户输入一组数字(=学生的标记)。此外,用户自己将决定阵列的大小,即课堂上的学生数量。接受了每个学生的分数后,我想将它们存储在一个数组中,显示所有学生的分数,并计算课程的平均分数。稍后会达到平均值。 有人可以用这个(显然是错误的)代码帮助我,谢谢!!
package chapter2.Arrays;
import java.util.*;
public class Arrays_ExampleOne {
static int size;
static int marks;
static int [] numbers=new int [size];
static Scanner in=new Scanner(System.in);
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Welcome to Cincinnati Elementary");
System.out.println("Class report details for ");
System.out.println("Enter the number of students in the class");
size=in.nextInt();
System.out.println("Enter the marks of each student");
for(int i=0;i<=size;i++)
{
numbers[i]=in.nextInt();
}
/* System.out.println("The marks of all students are"+ " ");
{
for(int i=0;i<=size;i++)
{
System.out.print(numbers[i]);
}
System.out.println();
}
//int avg=
*/}
}
=============================================== ============
修改代码,希望在main之外创建一个函数来计算数组的平均值:
package chapter2.Arrays;
import java.util.*;
public class Arrays_ExampleOne {
/*static int size;
static int marks;
static int [] numbers=new int [size];*/
static Scanner in=new Scanner(System.in);
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Welcome to Cincinnati Elementary");
System.out.println("Class report details for ");
System.out.println("Enter the number of students in the class");
int size;
//static int marks;
//static int [] numbers=new int [size];
size=in.nextInt();
int [] numbers=new int [size];
System.out.println("Enter the marks of each student");
for(int i=0;i<size;i++)
{
numbers[i]=in.nextInt();
}
System.out.println("The marks of all students are:"+ " ");
{
for(int temp:numbers)
{
System.out.print(temp+ " ");
}
System.out.println();
}
int arraySize=numbers.length;
int arraySum=0;
//Calculate the sum of array elements
for(int temp1:numbers)
{
arraySum+=temp1;
}
int average=arraySum/arraySize;
System.out.println("The average of all the students scores is:"+ " "+average);
}
/*public int calculateAverage(int array[])
{
int arraySize=array.length;
}*/
}
=============================================== =============== 在main之外添加了用于计算平均值的单独代码。
package chapter2.Arrays;
import java.util.*;
public class Arrays_ExampleOne {
/*static int size;
static int marks;
static int [] numbers=new int [size];*/
static Scanner in=new Scanner(System.in);
public static void main(String[] args) {
// TODO Auto-generated method stub
Arrays_ExampleOne obj=new Arrays_ExampleOne();
System.out.println("Welcome to Cincinnati Elementary");
System.out.println("Class report details for ");
System.out.println("Enter the number of students in the class");
int size;
//static int marks;
//static int [] numbers=new int [size];
size=in.nextInt();
int [] numbers=new int [size];
System.out.println("Enter the marks of each student");
for(int i=0;i<size;i++)
{
numbers[i]=in.nextInt();
}
System.out.println("The marks of all students are:"+ " ");
{
for(int temp:numbers)
{
System.out.print(temp+ " ");
}
System.out.println();
}
/* int arraySize=numbers.length;
int arraySum=0;
//Calculate the sum of array elements
for(int temp1:numbers)
{
arraySum+=temp1;
}
int average=arraySum/arraySize;
System.out.println("The average of all the students scores is:"+ " "+average);*/
int average=obj.calculateAverage(numbers);
System.out.println("The average of all the students is:"+ " "+average);
}
public int calculateAverage(int array[])
{
int arraySize=array.length;
int arraySum=0;
for(int temp: array)
{
arraySum+=temp;
}
int average=arraySum/arraySize;
return average;
}
}
答案 0 :(得分:4)
首先,您应该要求用户先输入大小,然后设置数组大小:
static int size;
static int marks;
static Scanner in=new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Enter number of student:");
size = in.nextInt();
int [] numbers=new int [size];
//the rest of your code goes here
}
注意:小心你的for循环:
for(int i=0;i<=size;i++){
^
numbers[i]=in.nextInt();
}
由于您尝试访问索引为ArrayIndexOutOfBoundException
的元素,结果为size
, n 大小的数组的最大index
为 n-1 。 应 <
而不是<=
对于方法calculateAverage,它应如下所示:
public static double calculateAverage(int array[])
{
double sum=0.0;
for(int i=0;i<array.length;i++){
sum+=array[i];
}
return sum / array.length;
}
请注意,此方法应该是静态的,因为您要在静态方法中使用它, average 始终处理double
/ float
数据类型,而不是{{1} }
从main方法中,您可以这样调用它:
int
答案 1 :(得分:0)
static int size;
static int marks;
static int [] numbers=new int [size];
在创建数组时,size
仍然是默认值,即零。你总是在这里得到一个长度为零的数组。