无效的方法声明问题 [英] Invalid method declaration issue

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

问题描述

为什么我在 check(values); 上收到无效的方法声明;需要返回类型"错误?

Why am I getting an "invalid method declaration; return type required" error on check(values); ?

public class Swap
{
    int[] values = {5, 6, 7, 8, 9};
    check(values);


    public void swapAdjacentElemnts(int[] values)
    {
        for(int i=0; i<values.length - 1; i+=2)
        {
            int tempInt = values[i];
            values[i] = values[i+1];
            values[i+1]=tempInt;
        }
    }

    public int[] check(int[] values)
    {
        swapAdjacentElements(values);
        return values;
    }
}

推荐答案

您正试图在方法之外执行代码.您对 check 的调用必须存在于某种方法内部,而不是类声明中.

You are attempting to execute code outside of a method. Your call to check has to live inside of some kind of method, rather than in the class declaration.

如果您打算在构造函数中使用它,您可以这样做:

If you meant for this to be in the constructor, you can do that:

public Swap()
{
    check(values);
}

这篇关于无效的方法声明问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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