简体   繁体   English

方法错误 - Java 中缺少 return 语句

[英]Method error - missing return statement in Java

I am trying to finish a linked list activity but I'm having an error message saying "missing return statement" right after the very last curly brace of this code.我正在尝试完成一个链表活动,但在此代码的最后一个大括号之后,我收到一条错误消息,提示“缺少返回语句”。 What's wrong with this?这有什么问题?

public boolean insertNode(DataElement insertItem)
{
    LinkedListNode current;
    LinkedListNode trailCurrent;
    LinkedListNode newNode;

    boolean found;

    newNode = new LinkedListNode();
    newNode.info = insertItem.getCopy();

    newNode.link = null;

    if(first == null)
    {
        first = newNode;
        count++;
    }
    else
    {
        trailCurrent = first;
        current = first;
        found = false;

        while(current !=null && !found)
            if(current.info.compareTo(insertItem) >= 0)
                found = true;
            else
            {
                trailCurrent = current;
                current = current.link;
            }
        if(current == first)
        {
            newNode.link = first;
            first = newNode;
            count++;
        }
        else
        {
            trailCurrent.link = newNode;
            newNode.link = current;
            count++;
        }
    }
}
public boolean insertNode 

Three parts in that declaration:该声明中的三个部分:

public -- Tells about the accessibility of this method. public -- 说明此方法的可访问性。 Public means that any class can access it.公共意味着任何类都可以访问它。

boolean -- The method should return a boolean value to the caller. boolean -- 该方法应该向调用者返回一个布尔值。 You can use void if you dont want to return anything.如果您不想返回任何内容,可以使用void

insertNode -- name of the method. insertNode -- 方法的名称。

So a method is constructed in the following way:所以一个方法是通过以下方式构造的:

Accesscontrol returnType methodName

Can have following values:可以有以下值:

AccessControl: private, public, protected
returnType: int, boolean, String, void, List etc etc -- Limited just by imagination. 
methodName - should be meaningful verb. 

So when you ask your method to return a boolean, you call it the following way:所以当你要求你的方法返回一个布尔值时,你可以这样调用它:

boolean insertSuccessful = insertNode(dataElement); 

now insertSuccessful will contain a true or false value, depending if the insert was successful.现在 insertSuccessful 将包含 true 或 false 值,具体取决于插入是否成功。

If your method was void, it would return nothing and the caller wouldn't expect a value to be returned and hence no insertSuccessful variable.如果您的方法是无效的,它将不返回任何内容,并且调用者不会期望返回值,因此没有 insertSuccessful 变量。

public boolean insertNode

returns a boolean.返回一个布尔值。 If you want to do something in a function without returning anything, just replace如果您想在函数中执行某些操作而不返回任何内容,只需替换

public void insertNode 

Return value is SUPER important in all programming languages.返回值在所有编程语言中都非常重要。 Get used to always put the correct return value of a function (If you precised it on the function's prototype).习惯于始终放置函数的正确返回值(如果您在函数原型上精确地说明了它)。

In your case, you should return the only boolean variable you're using在您的情况下,您应该返回您正在使用的唯一布尔变量
return found;

//by using void return type //通过使用void返回类型

public static void diff(int x, int y) {公共静态无效差异(int x,int y){

//calculate difference //计算差值

      int z = x - y; 

//print the difference here instead of the main() method // 在此处打印差异而不是 main() 方法

     System.out.println(z);  

} }

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

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