gpt4 book ai didi

java - Java中通过数组接受输入

转载 作者:行者123 更新时间:2023-12-01 18:36:06 25 4
gpt4 key购买 nike

我试图让用户输入一组数字(=学生的分数)。另外,用户自己将决定数组的大小,即类(class)学生的数量。接受每个学生的分数后,我想将它们存储在一个数组中,显示所有学生的分数,并计算类(class)的平均分。稍后将谈到平均部分。有人可以帮我解决这段(显然是错误的)代码吗,谢谢!!

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;
}
}

最佳答案

首先,您应该要求用户先输入大小,然后设置数组大小:

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 处的元素,其中最大值 index n 大小的数组为 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;
}

请注意,此方法应该是静态的,因为您想在静态方法中使用它,并且平均始终处理 double/float数据类型,而不是 int

从 main 方法中,您可以这样调用它:

System.out.println("Average marks:"+calculateAverage(numbers));

关于java - Java中通过数组接受输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21948221/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com