将值返回到main方法

时间:2015-11-02 04:54:14

标签: java methods

好的,这让我的大脑融化了!!代码编译得很好,但它拒绝在displayAllResults方法中显示正确的答案。我不知道如何解决这个问题。我已经尝试将这些方法设为私有,并让它们返回值而不是无效。例如,方法sum获取数组中元素的总和但不显示它们。我得到了0.

//Main
public class Lab_4_Practice {

  public static void main(String[] args) {

    //Declaring and initializing variables
    int[] randomArray = new int[10];
    int maxIndex = 0;
    int minIndex = 0;
    int total = 0;
    double average = (total / randomArray.length);  

    //Call Methods
    random(randomArray);
    displayRandom(randomArray);
    largest(maxIndex, randomArray);
    smallest(minIndex, randomArray);
    sum(total, randomArray);
    average(total, randomArray);
    sortArray(randomArray);
    displaySorted(randomArray); 
    displayAllResults(randomArray, maxIndex, minIndex, total, average);      
  }
  //***************************************************
  //Method assigns random values to elements
  public static void random(int[] randomArray) {

    for (int i = 0; i <randomArray.length; i++) {
      randomArray[i] = (int)(Math.random() * 300); 
    } 
  }

  //Method prints random values 
  public static void displayRandom(int[] randomArray) {

    System.out.println("Here are 10 random numbers");
    for (int i = 0; i < randomArray.length; i++) {

      System.out.println(randomArray[i]);
    }
      System.out.println("*************************");
  }

  //Method identifies largest index and its element in array
  public static void largest(int maxIndex, int[] randomArray) {

      for (int l = 1; l < randomArray.length; l++) {
        if (randomArray[l] > randomArray[maxIndex]) {
          maxIndex = l;
        }  
      }  
  }

  //Method identifies smallest index and its element in array
  public static void smallest(int minIndex, int[] randomArray) {


      for (int i = 1; i < randomArray.length; i++) {
        if (randomArray[i] < randomArray[minIndex]) {
          minIndex = i;
        }
      }   
  }

   //Method calculates sum of elements
  public static int sum(int total, int[] randomArray) {


    for (int i = 0; i <randomArray.length; i++) {
      total = total + randomArray[i]; 
    } 
    return total;
  } 

  //Method calculates average of elements
  public static void average(int total, int[] randomArray) {


    for (int i = 0; i < randomArray.length; i++) {
      i += randomArray[i];

    }
  }

  //Method sorts array in ascending order
  public static void sortArray(int[] randomArray) {

    for (int i = 0; i < randomArray.length - 1; i++) {
      int currentMin = randomArray[i];
      int currentMinIndex = i;

      for (int j = i + 1; j < randomArray.length; j++) {
        if (currentMin > randomArray[j]) {
          currentMin = randomArray[j];
          currentMinIndex = j;
        }
      }

      if (currentMinIndex != i) {
        randomArray[currentMinIndex] = randomArray[i];
        randomArray[i] = currentMin;
      }
    } 
  }

  //Method prints array in ascending order  
  public static void displaySorted(int[] randomArray) {

    System.out.println("These are the same numbers sorted in ascending order");
    for (int i = 0; i < randomArray.length; i++) {
      System.out.println(randomArray[i] + " ");
    }
      System.out.println("*************************");
  }

  //Method prints results of largest smallest sum and average
  public static void displayAllResults(int[] randomArray, int maxIndex, int minIndex, int total, double average) {

    System.out.println("The largest index is " + maxIndex + " and its value is " + randomArray[maxIndex]);
    System.out.println("The smallest index is " + minIndex + " and its value is " + randomArray[minIndex]);
    System.out.println("The sum of the elements is " + total);
    System.out.println("The average of the elements is " + average);

  }
}

4 个答案:

答案 0 :(得分:0)

它始终建议您在不同的类中进行所有计算/操作,而不是在主类本身中进行。创建一个不同的类,在这个代码里面就像这样 -

public class Example{
public void assign(int[] Array){
    for(int i=0;i<Array.length;i++){
        Array[i]=(int)(Math.random()*300);
    }
}

public void display(int[] Array){
    System.out.println("The 10 elements of the array are:");
    for(int i=0;i<Array.length;i++){
        System.out.println(Array[i]);
    }
}

public int sum(int[] Array) {
    int total =0;
    for(int i=0;i<Array.length;i++){
        total=total+Array[i];
    }
    return total;
}
//write all other methods here in this class.
}

现在在main方法的main类中只是声明数组并根据你的要求将数组传递给不同的函数,如下所示 -

public static void main(String[] args) {
    int[] randomArray=new int[10];
    Example e=new Example();
    e.assign(randomArray);//this must be called first to assign the values inside the array.
    e.display(randomArray);//call this method if you wish to display the values.
    System.out.println("The sum of the elements are: "+e.sum(randomArray));
}

答案 1 :(得分:0)

我在代码中做了一些改动。您可以将其与旧代码进行比较。由于局部变量,您面临的大多数地方都存在问题。无论您向相应的方法提供什么,在操作之后对局部变量进行的更改都不会影响您的实例变量。对于max()和small()方法(),你在不对数组进行排序的情况下调用它,因为它输出错误。

public class StackProblem {     public static void main(String [] args){

    // Declaring and initializing variables
    int[] randomArray = new int[10];
    int maxIndex = 0;
    int minIndex = 0;
    int total = 0;
    double average = 0;

    // Call Methods
    random(randomArray);
    displayRandom(randomArray);
    sortArray(randomArray);

    maxIndex=largest(randomArray);
    minIndex=smallest(randomArray);

    total=sum(randomArray);
    average=average(total, randomArray);

    displaySorted(randomArray);
    displayAllResults(randomArray, maxIndex, minIndex, total, average);
}

// ***************************************************
// Method assigns random values to elements
public static void random(int[] randomArray) {

    for (int i = 0; i < randomArray.length; i++) {
        randomArray[i] = (int) (Math.random() * 300);
    }
}

// Method prints random values
public static void displayRandom(int[] randomArray) {

    System.out.println("Here are 10 random numbers");
    for (int i = 0; i < randomArray.length; i++) {

        System.out.println(randomArray[i]);
    }
    System.out.println("*************************");
}

// Method identifies largest index and its element in array
public static int largest(int[] randomArray) {
    int maxIndex=0;
    for (int l = 0; l < randomArray.length; l++) {
        if (randomArray[l] > randomArray[maxIndex]) {
            maxIndex = l;
        }
    }
    return maxIndex;
}

// Method identifies smallest index and its element in array
public static int smallest(int[] randomArray) {
    int minIndex=0;
    for (int i = 0; i < randomArray.length; i++) {
        if (randomArray[i] < randomArray[minIndex]) {
            minIndex = i;
        }
    }
    return minIndex;
}

// Method calculates sum of elements
public static int sum(int[] randomArray) {
    int localTotal=0;
    for (int i = 0; i < randomArray.length; i++) {
        localTotal+=randomArray[i];
    }
    return localTotal;
}

// Method calculates average of elements
public static int average(int total, int[] randomArray) {

    return total/randomArray.length;
}

// Method sorts array in ascending order
public static void sortArray(int[] randomArray) {

    for (int i = 0; i < randomArray.length - 1; i++) {
        int currentMin = randomArray[i];
        int currentMinIndex = i;

        for (int j = i + 1; j < randomArray.length; j++) {
            if (currentMin > randomArray[j]) {
                currentMin = randomArray[j];
                currentMinIndex = j;
            }
        }

        if (currentMinIndex != i) {
            randomArray[currentMinIndex] = randomArray[i];
            randomArray[i] = currentMin;
        }
    }
}

// Method prints array in ascending order
public static void displaySorted(int[] randomArray) {

    System.out.println("These are the same numbers sorted in ascending order");
    for (int i = 0; i < randomArray.length; i++) {
        System.out.println(randomArray[i] + " ");
    }
    System.out.println("*************************");
}

// Method prints results of largest smallest sum and average
public static void displayAllResults(int[] randomArray, int maxIndex, int minIndex, int total, double average) {

    System.out.println("The largest index is " + maxIndex + " and its value is " + randomArray[maxIndex]);
    System.out.println("The smallest index is " + minIndex + " and its value is " + randomArray[minIndex]);
    System.out.println("The sum of the elements is " + total);
    System.out.println("The average of the elements is " + average);

}

答案 2 :(得分:0)

[由于[重复],没有人支持我的动议暂停 - 所以我觉得有必要在这里写一个答案。]

这是从数组元素计算某个值的方法的典型模式:

public static int largest(int[] array) {
    int maxIndex = 0;
    for (int l = 1; l < array.length; l++) {
        if (array[l] > array[maxIndex]) {
            maxIndex = l;
        }  
    }
    return maxIndex;
}

你这样称呼它:

int maxIndex = largest( randomArray );

具有int,long,double,char,short,float,boolean类型的参数被“按值”复制,即,计算调用中的相应表达式 - 实际参数 - 并将结果存储在变量与参数的名称。这个变量是短暂的:当你从方法调用返回时,它就消失了。返回值的唯一方法是使用非void方法中的返回机制。

如果你有一个阵列,那么事情会有所不同。数组是一个对象,对象通过引用处理,这些值告诉计算机可以找到对象的位置。此引用也从调用点复制并存储在短期变量中,但您仍然可以访问它引用的对象,以便可以通过代码更改数组(或任何对象)在具有对象引用作为参数的方法中。

最后,计算平均值的代码非常错误。

public static double average( int[] array ) {
    return (double)sum( array )/array.length;
}

并且您需要一个double类型的变量来保存结果。 (阵列有可能长度为0,所以我在那里有点粗心。你明白为什么吗?)

答案 3 :(得分:-1)

传递给每个方法的变量都是零。您需要在main方法中更改它们,因为方法中的变量仅在其各自的方法中使用和修改,即使它们是从main方法传递给该方法的。看起来这就是你想要的:

randomArray = random(randomArray);
displayRandom(randomArray);
maxIndex = largest(maxIndex, randomArray);
minIndex = smallest(minIndex, randomArray);
total = sum(total, randomArray);
average(total, randomArray); //not sure what you're trying to do here; this method does not calculate the average
average = total/randomArray.length; //you probably just want to use this instead of the average() method
randomArray = sortArray(randomArray);
displaySorted(randomArray); 
displayAllResults(randomArray, maxIndex, minIndex, total, average);

此外,对于应该返回值的每个方法,您需要将方法标题中的返回类型从void更改为适当的变量类型,并添加return每种方法结束时的陈述。