我有一个家庭作业,我必须创建一个数组,对数组中的元素求和,然后找到数组中元素的平均值,但我必须采用与求和方法相同的方法。
这是我到目前为止所做的,我已经查看了我的书并将其放在我的代码中,但它不能正确编译。
class SumArray{
public static void main(String args[]){
double[] array = {7,8,9,10,11,12,13};
sumArray(array);
}
public static double sumArray(double[] array){
double sum = 0;
for ( int i = 0; i < array.length; i++){
sum += array[i];
//System.out.println("Sum is = " + sum);
}
//System.out.println("Sum is = " +sum);
//after summing then get average
return sum;
double average = sum/array.length;
System.out.println("Average value of array element is " + average);
return average;
}
}
答案 0 :(得分:1)
您正在使用方法return sum;
返回总和,因此在使用方法编写的代码无法运行之后。这可能是错误。
简单的解决方案是创建一种不同的方法来计算平均值。
class SumArray{
public static void main(String args[]){
double[] array = {7,8,9,10,11,12,13};
double results[] = sumArray(array);
System.out.println("Sum "+result[0]);
System.out.println("Average "+result[1]);
}
public static double[] sumArray(double[] array){
double sum = 0;
for ( int i = 0; i < array.length; i++){
sum += array[i];
//System.out.println("Sum is = " + sum);
}
//System.out.println("Sum is = " +sum);
//after summing then get average
double average = sum/array.length;
System.out.println("Average value of array element is " + average);
double results[] = {sum, average};
return results;
}
}
答案 1 :(得分:1)
您的代码无法编译,因为在return
语句之后不能有其他语句或声明,因为该代码肯定无法访问。
如果必须在一个方法中执行,则您的方法必须返回2个值。你可以用不同的方式做到这一点:
1)将2个值作为数组返回:
// The 1st element in the returned array is the sum, the 2nd is the average
public double[] sumAvg(double[] arr) {
double sum = 0;
for (double d : arr)
sum += d;
return new double[]{sum, sum/arr.length};
}
2)为此创建一个包装类:
class SumAvg {
public double sum, avg;
}
public SumAvg sumAvg(double[] arr) {
double sum = 0;
for (double d : arr)
sum += d;
SumAvg sa = new SumAvg();
sa.sum = sum;
sa.avg = sum / arr.length;
return sa;
}
3)将2个值作为List
返回
// The 1st element in the returned List is the sum, the 2nd is the average
public List<Double> sumAvg(double[] arr) {
double sum = 0;
for (double d : arr)
sum += d;
return Arrays.asList(sum, sum/arr.length);
}
答案 2 :(得分:1)
好吧,您只需删除return sum
即可。你的代码完全没问题。
同时你也可以用这种方式找到数组的总和和平均值,
public class AverageArray
{
public static void main(String[] args)
{
int num[] = new int[]{30,12,14,24,19,89,128};
int total = 0;
for(int a = 0; a < num.length; a++)
{
total = total + num[a];
}
double average = total / num.length;
System.out.println("The total is : " + total);
System.out.println("Java average of array : " + average);
}
}
来源和参考 - Sum and average of an array。
答案 3 :(得分:1)
在回答之前,了解 return 语句的用法非常重要。此外,您需要了解无法访问的代码的含义。为了理解这一点,我在这里与您分享一个例子。考虑以下代码:
public static void main(String args[]){
String returnedString;
returnedString=testReturnStatement("Rainy");
System.out.println(returnedString);
returnedString=testReturnStatement("Sunny");
System.out.println(returnedString);
returnedString=testReturnStatement("Can't figure out the weather");
System.out.println(returnedString);
}
现在暂时考虑一下如下所示的功能:
public static String testReturnStatement(String currentCondition){
return "you can go by Car or taxi"; //Line no.1
return "You can go by Motorbike or cycle"; //Line no.2
return "You can always use public transport"; //Line no.3
}
这是一个与您的情况类似的return语句列表。 在这里,第2行将给出无法访问的代码的错误,因为在第1行之后,控件将被发送回main方法。 return下面的语句根本不会执行。这就是为什么它显示无法访问的代码(通常称为无效代码)的原因。
但是,并不意味着,您不能在方法中使用 条件回报 。 您可以使用不同的return语句,直到它们处于不同的条件下为止。 最后,应该只有一个值可以返回到调用它的位置。
public static String testReturnStatement(String currentCondition){
if(currentCondition.equals("Rainy")){
return "you can go by Car or taxi";
}else if(currentCondition.equals("Sunny")){
return "You can go by Motorbike or cycle";
}
return "You can always use public transport";
}
因此,在您的代码中发现了非常相同的问题,即return语句放置不正确,因此您无法编译该代码。从代码中删除return sum;
会删除您的错误。为了方便起见,我将从您的代码中删除return sum;
并将其添加到此处。
public static void main(String args[]){
double[] array = {30,12,14,24,19,89,128};
sumArray(array);
}
public static double sumArray(double[] array){
double sum = 0;
for ( int i = 0; i < array.length; i++){
sum += array[i];
//System.out.println("Sum is = " + sum);
}
System.out.println(sum);
//System.out.println("Sum is = " +sum);
//after summing then get average
double average = sum/array.length;
System.out.println("Average value of array element is " + average);
return average;
}
答案 4 :(得分:0)
由于您上方已有return
语句
double average = sum/array.length;
您只需删除return sum;
答案 5 :(得分:0)
制作sum
和average
个实例变量,并在方法中为它们赋值。因此,您可以在单个方法中计算sum
和average
。
public class Average {
double sum,average;
public static void main(String[] args) {
double[] numbers = {1,2,3,4,5,6,7};
sumAndAverage(numbers);
System.out.println("Average is " + this.average+" and sum is "+this.sum);
}
public static void sumAndAverage(double[] numbers)
{
for (int i = 0; i < numbers.length; i++) {
this.sum += numbers[i];
}
this.average = this.sum / numbers.length ;
}
}
答案 6 :(得分:0)
public class ToFindAverage {
public static void main(String[] args) {
double total;
double average;
int i;
int[] list;
list = new int[4]; //make container for 4
list[0] = 1;
list[1] = 2;
list[2] = 3;
list[3] = 4;
total = 0;
for (i = 0; i < list.length; i++) {
total = total + list[i];
}
average = total / list.length;
System.out.println("The total average is " + average);
}
}
答案 7 :(得分:0)
认为你应该添加更多的OOP。创建这样的课程,你的老师会留下深刻的印象,会给予金钱奖励,并将你送到IT竞赛中:
class MathArray {
private double[] data;
public MathArray(double[] data) {
this.data = data;
}
public double getSum() {
double out = 0;
for(double d : data) {
out += d;
}
return out;
}
public double getAverage() {
return getSum() / data.length;
}
}