简体   繁体   English

不明白错误讯息

[英]Don't understand error message

public class ArrayPrac {

    public static void main(String[] args) {
        int[] arrayOne = {2, 3, 4, 5, 6};
        System.out.println(findMin(arrayOne));
    }

    public static void findMin(int[] list) {
        int minValue = list[0];
        int i = 1;      
        for( i = 1; 1 < list.length; i++);
            if(list[i] < minValue) {
                minValue = list[i];

            }
    }     
}

In the System.out.print part in line 6 it wont run and gives me the compiler error: 在第6行的System.out.print部分中,它不会运行,并给我编译器错误:

The method println(boolean) in the type PrintStream is not applicable for the arguments (void) 类型PrintStream的方法println(boolean)不适用于参数(void)

I seem to have searched for an answer all day, so now I post my specific case. 我似乎整天都在寻找答案,所以现在发布我的具体案例。

Cheers. 干杯。

Fix this, at the end of your findMin() method you must return the minimum value that was found: 解决此问题,在findMin()方法的末尾,您必须返回找到的最小值:

return minValue;

And consequently, the method signature must be changed, too: 因此,方法签名也必须更改:

public static int findMin(int[] list)

It makes sense: if the findMin() method does all that hard work to find the minimum value, the end result must not be left as a local variable, it won't be useful outside if you don't return it after the method invocation ends. 这是有道理的:如果findMin()方法findMin()来寻找最小值,则最终结果一定不能findMin()局部变量,如果您在方法后不返回它,则外部结果将无用调用结束。

There's another hard-to-find bug lurking, by the way. 顺便说一下,还有一个难以发现的错误。 Remove the ; 删除; at the end of the line with the for , and put the contents of the loop inside a pair of {} . for的末尾,并将循环的内容放在一对{} Currently, the loop is empty, and the lines after the for lie outside the loop. 当前,循环为空, for之后的行位于循环外部 And the loop condition is wrong, too! 而且循环条件也是错误的! here's how the method should look after all the problems are fixed: 解决所有问题后,该方法应如下所示:

public static int findMin(int[] list) {
    int minValue = list[0];
    for (int i = 1; i < list.length; i++) {
        if (list[i] < minValue) {
            minValue = list[i];
        }
    }
    return minValue;
}

System.out.println takes a String input but you are passing a void . System.out.println接受String输入,但您传递的是void As your method findMin returns void. 当方法findMin返回void时。 This is causing the compiler error. 这导致了编译器错误。

Now speaking about the logical problem, you may want to display the output of findMin method but the method does not return anything. 现在谈论逻辑问题,您可能想要显示findMin方法的输出,但是该方法不返回任何内容。 So returning minValue may make sense here. 因此,在这里返回minValue可能很有意义。

Once you return the int value from minValue method then you can display the result by concatenating it to a an empty string. 一旦从minValue方法返回int值,则可以通过将其串联为空字符串来显示结果。 Something like this: 像这样:

    System.out.println("" + findMin(arrayOne));

The method findMin() is declared as returning type void : Either declare it to return something (and return something), 方法findMin()声明为返回类型void :要么声明它以返回某种内容(然后返回某种内容),

public static int findMin(int[] list) {
    int minValue = list[0];
    int i = 1;      
    for( i = 1; 1 < list.length; i++)
        if(list[i] < minValue) {
            minValue = list[i];
        }
    return minValue;
}

Note fixing bug: removing semicolon from after for() 注意修复错误:从for()之后删除分号

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

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