不明白错误讯息 [英] Don't understand error message

查看:54
本文介绍了不明白错误讯息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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];

            }
    }     
}

在第6行的System.out.print部分中,它不会运行,并给我编译器错误:

In the System.out.print part in line 6 it wont run and gives me the compiler error:

PrintStream类型的方法println(boolean)不适用于参数(void)

The method println(boolean) in the type PrintStream is not applicable for the arguments (void)

我似乎整天都在寻找答案,所以现在我发布我的具体案例.

I seem to have searched for an answer all day, so now I post my specific case.

干杯.

推荐答案

解决此问题,在您的 findMin()方法末尾,您必须返回找到的最小值:

Fix this, at the end of your findMin() method you must return the minimum value that was found:

return minValue;

因此,方法签名也必须更改:

And consequently, the method signature must be changed, too:

public static int findMin(int[] list)

这是有道理的:如果 findMin()方法竭尽全力寻找最小值,则最终结果一定不能留作局部变量,在外部不会有用如果在方法调用结束后没有返回它.

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.

顺便说一句,还有另一个难以发现的错误.用 for 删除行末的; ,然后将循环的内容放入一对 {} 中.当前,该循环为空,并且 for 之后的行位于该循环的 外.而且循环条件也是错误的!解决所有问题后,该方法应如下所示:

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 {}. Currently, the loop is empty, and the lines after the for lie outside the loop. 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;
}

这篇关于不明白错误讯息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆