简体   繁体   English

从java中的int []方法返回单个int

[英]Returning a single int from int[] method in java

I am trying to make a method 'difference' that finds the largest number in an int array and returns the int to the main method.我正在尝试创建一个方法“差异”,该方法在 int 数组中查找最大数并将 int 返回给 main 方法。 I am getting an error telling me I can't convert an int array to an int.我收到一条错误消息,告诉我我无法将 int 数组转换为 int。 How can I make this work?我怎样才能使这项工作? I don't want to return an entire array.我不想返回整个数组。 Thanks a lot非常感谢

public class bigDiff{
    public static void main(String[] args){
        int[] arr = new int[] {7, 6, 8, 9};
        difference(arr);
    }

    public static int difference (int[] input){
        int smallest = input[0];
        int largest = input[0];

        for (int i = 0; i < input.length; i++){
            if(input[i] >= largest){
                largest = input[i];
            } else if(input[i] <= input[largest]){
                smallest = input[i];
            }
        } 
        return largest;

    }   
}

watch this line of code看这行代码

else if(input[i] <= input[largest]){
            smallest = input[i];
        }

input[largest] , first time largest value is 7 means input[7],and your array size is only 4.so this not work. input[largest] ,第一次最大值是 7 表示 input[7],你的数组大小只有 4.so 这不起作用。

input[largest] instead use input[i] . input[largest]改为使用input[i]

I'll try to explain a few things:我将尝试解释一些事情:
First, your declaration of the array is quite weird, you should declare: int[] arr = {7, 6, 8, 9};首先,你对数组的声明很奇怪,你应该声明: int[] arr = {7, 6, 8, 9};
Second, difference function returns an int, what are you doing with it?其次, difference函数返回一个整数,你用它做什么? nothing.. try printing it.. System.out.println(difference(arr));没什么.. 尝试打印它.. System.out.println(difference(arr));
Third, you're trying to get the largest number, why do you need the smallest number?第三,你想得到最大的数字,为什么你需要最小的数字? Even the compiler warns you for a variable which isn't being used.甚至编译器也会警告您未使用的变量。 So your difference function should be like:所以你的difference函数应该是这样的:

int largest = input[0];
for (int i = 0; i < input.length; i++){
    if(input[i] > largest)
        largest = input[i];
} 
return largest;

** You don't need to do anything if input[i] == largest , so checking if(input[i] > largest) is enough. ** 如果input[i] == largest ,则不需要执行任何操作,因此检查if(input[i] > largest)就足够了。

Good Luck :)祝你好运 :)

You have declared an array ie int[] arr = new int[] {7, 6, 8, 9} length of this array is 4 but here in the difference function,您已经声明了一个数组,即int[] arr = new int[] {7, 6, 8, 9}这个数组的长度是 4 但在差异函数中,

for (int i = 0; i < input.length; i++){
        if(input[i] >= largest){
            largest = input[i];
        } else if(input[i] <= input[largest]){
            smallest = input[i];
        }
    } 

when you are comparing this in else if(input[i] <= input[largest]) then it will generate error because in the first iteration largest=7 and in next iteration it will go in else if where input[1]<=input[7] is checked so the array out of bound exception will be generated.当您在else if(input[i] <= input[largest]) 中进行比较时,它将产生错误,因为在第一次迭代中最大 = 7并且在下一次迭代中它将进入 else if where input[1]<= input[7]被检查,因此将生成数组越界异常。

I think you want to get the largest element in the array so instead of doing this you can short the array and access the largest element easily.我认为您想获得数组中的最大元素,因此您可以缩短数组并轻松访问最大元素,而不是这样做。

Problems:问题:
1. largest is used as an index (ie, input[largest] ), which may cause ArrayOutOfBoundsException if largest is larger than the array length 2. largest is used in the if to set smallest 1. largest用作索引(即input[largest] ),如果largest大于数组长度可能会导致ArrayOutOfBoundsException 2. largest用于if设置smallest
3. difference returns the largest element instead of the difference between the largest and smallest elements 3. difference返回largest元素而不是largestsmallest元素之间的差异
4. Return value of difference isn't used (not a real problem, but you can't see the output of difference ) 4. 不使用difference返回值(不是真正的问题,但您看不到difference的输出)

Otherwise the (amended) program below works:否则,以下(修改后的)程序有效:

public class bigDiff{
    public static void main(String[] args){
        int[] arr = new int[] {7, 6, 8, 9};
        System.out.println(difference(arr)); // problem 4 was here
    }

    public static int difference (int[] input){
        int smallest = input[0];
        int largest = input[0];

        for (int i = 0; i < input.length; i++){
            if(input[i] >= largest){
                largest = input[i];
            } else if(input[i] <= smallest){ // problems 1 and 2 were here
                smallest = input[i];
            }
        } 
        return largest - smallest; // problem 3 was here

    }   
}

This should work just fine:这应该可以正常工作:

public static int difference (int[] input){
    int smallest = input[0];
    int largest = input[0];

    for (int i = 0; i < input.length; i++){
        if(input[i] > largest){
            largest = input[i];
        } else if(input[i] < smallest){
            smallest = input[i];
        }
    } 
    return largest;

}  

Also there is no point in keeping the smallest value when you're interested only in the largest.当您只对最大值感兴趣时,保留最小值也没有意义。

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

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