简体   繁体   English

从java中的方法返回值时出错

[英]error with returning a value from a method in java

hello in the code below I am sorting (sort method) a small array to find the largest number.你好在下面的代码中我正在排序(排序方法)一个小数组以找到最大的数字。 I then print the answer in the (display method).然后我在(显示方法)中打印答案。

But to extend my knowledge I want to mass the max value back to them main in a return statement and then print from there....simply to learn how to return a value.但是为了扩展我的知识,我想在 return 语句中将最大值返回给它们 main,然后从那里打印......只是为了学习如何返回一个值。

package christmas;

public class maxvalue 
{ 
    public static void main(String[] args) 
    { 
        int[] data={10,90,30}; 
        sort(data); 
        System.out.println("\nmax number is :"); 
        display(data); 
        System.out.println(data);
    } 
    static int display(int num[]) 
    { 
        System.out.print(num[0] + " "); 
        return num[0];
    }
    static void sort(int num[]) 
    { 
        int i, j, temp; 
        for(i=0; i<num.length-i;i++) 
        {  
            for(j=0; j<num.length-i-1;j++) 
            { 
                if(num[j]<num[j+1]) 
                { 
                    temp = num[j]; 
                    num[j] = num[j+1]; 
                    num[j+1] = temp; 
                }
            }
        }
    }
}

the output is:输出是:

max number is : 90 [I@4617c264最大数量为:90 [I@4617c264

90 is the max value as printed by the display value. 90 是显示值打印的最大值。 But after this I have a return of the max value and then I try and print the return.但是在此之后,我返回了最大值,然后尝试打印返回值。 But instead of an integer it looks like a memory location is being printed.但它看起来不是一个整数,而是一个内存位置正在打印。

Any ideas please - I am student but this is not homework - simply trying to catch up.请有任何想法 - 我是学生,但这不是家庭作业 - 只是想赶上。 I appreciate that there are more elegant ways to calculate the max value in an array but what I am trying to learn is the passing of arguments to and from a method.我很欣赏有更优雅的方法来计算数组中的最大值,但我想学习的是在方法之间传递参数。

原因是您试图在最后一个 System.out 中打印数据,它是一个数组,这就是您看到内存地址的原因。尝试打印 display(data),您将看到输出所需的数字。

Instead of printing the returned value, you are printing the data array memory location:您打印的是data数组内存位置,而不是打印返回值:

System.out.println(data);

You should change that line with:您应该更改该行:

System.out.println(display(data));

With that line we have:有了这条线,我们有:

  1. Your display method is called and it prints the max value您的display方法被调用并打印最大值
  2. Your display method returns the max value您的display方法返回最大值
  3. println takes that returned value and prints it println获取返回值并打印它

try System.out.println(data[0]);尝试 System.out.println(data[0]); data is your array therefore printing data without an index will only print its memory location data 是您的数组,因此打印没有索引的数据只会打印其内存位置

private static int sort(int[] array){
  int a, b, max = 0;
  for (int i = 1;//If i was 0, it would have thrown an ArrayIndexOutOfBoundsException.
      i < array.length; i++){
    a = array[i-1];//The first element in the array.
    b = array[i];//The second one, and so on.
    if (a > b) max = a;//Check the bigger number.
    else max = b;
  }
  return max;
}
private static void display(int nr){
  System.out.print(nr);//Or what you want to do with the number.
}
public static void main(String[] args){
  int[] data={10,90,30};
  display(sort(data));
  //Or do it like this.
  int max = sort(data);
  display(max);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM