简体   繁体   English

“此方法必须返回int类型的结果” Java吗?

[英]'This method must return a result of type int' Java?

public static void main(String args[])
{
    double arr[] = {1,-6.3,9000,67.009,1.1,0.0,-456,6,23,-451.88};

    ArrayList<Integer> List = new ArrayList<Integer>();

     List.add(1);
     List.add((int) -6.3);
     List.add(9000);
     List.add((int) 67.009);
     List.add((int)1.1);
     List.add((int)0.0);
     List.add(-456);
     List.add(6);
     List.add(23);
     List.add((int)451.88);
}

public static int ArrayListMax(ArrayList List)
{

    for (int i=0; i<List.size(); ++i)
    {
        System.out.println(List.get(i));
    }

The error is in: 错误是在:

  public static int ArrayListMax(ArrayList List) 

This is probably a very nooby mistake, but I'm new to Java so forgive me. 这可能是一个非常大的错误,但是我是Java新手,请原谅我。

Any help please? 有什么帮助吗?

Thank you. 谢谢。

EDIT: 编辑:

I want the ArrayListMax method to print the size of the List! 我希望ArrayListMax方法打印列表的大小!

Either add a return statement to your ArrayListMax() method that returns an int or change the method signature from public static int to public static void . 在返回int ArrayListMax()方法中添加return语句,或者将方法签名从public static int更改为public static void And add a closing } to the method too. 并在方法中添加一个结束}

Also, you shouldn't use List as a name for the argument to that method because it's the name of an interface that you're actually importing in this code. 另外,您不应使用List作为该方法的参数名称,因为它是您实际上在此代码中导入的接口的名称。 The convention in java is for variable names and method names to begin with a lowercase letter (camel case) and class names to begin with an uppercase letter (pascal case). Java中的约定是变量名和方法名以小写字母(驼峰式)开头,而类名以大写字母(pascal字母)开头。

Assuming you are trying to get the maximum value in your List in the method arrayListMax , you need to return an integer in accordance with your method signature, which the error is telling you 假设您尝试在arrayListMax方法的List中获取最大值,则需要根据方法签名返回一个整数,该错误告诉您

This method must return a result of type int

Instead of printing all the values in the list, you could do: 您可以执行以下操作,而不是打印列表中的所有值:

public static int arrayListMax(List<Integer> List) {
   return Collections.max(list);
}

Use Java Naming Conventions . 使用Java命名约定 Method & variable names begin with a lowercase letter. 方法和变量名以小写字母开头。 Using this approach helps avoid confusion between instances & types (eg in the case of List in the main method). 使用这种方法有助于避免实例和类型之间的混淆(例如,在main方法中使用List的情况下)。

Problem is in this method: 问题在于这种方法:

public static int ArrayListMax(ArrayList List)
{

    for (int i=0; i<List.size(); ++i)
    {
       System.out.println(List.get(i));
    }
}

You are not returning from this method, you must return of type int to solve that error. 您不是从此方法返回的,必须返回int类型才能解决该错误。 Since you have specified int as return type. 由于您已将int指定为返回类型。

You created the function as returning a result 您将函数创建为返回结果

public static int ArrayListMax(ArrayList List) 

In order to remove your problem, if you need not return anything, write 为了解决您的问题,如果您不需要返回任何内容,请写

public static void ArrayListMax(ArrayList List) 

void means you do not want the method to return a result. void表示您不希望该方法返回结果。

如果您只是打印出值,则签名应为公共静态void ArrayListMax(ArrayList List)。

First , you are not returning anything with ArrayListMax method even if you have set its retun type to int . 首先 ,即使您将其retun类型设置为int ,也不会使用ArrayListMax方法返回任何内容。 Either return void as you are directly printing from the list. 直接从列表中打印时,要么返回void

Second, even if you set the correct return type to ArrayListMax method, you will still need to call that method in the main method as below: 其次,即使您为ArrayListMax方法设置了正确的返回类型,您仍然需要在main方法中调用该方法,如下所示:

public static void main(String args[])
{
    double arr[] = {1,-6.3,9000,67.009,1.1,0.0,-456,6,23,-451.88};

    ArrayList<Integer> List = new ArrayList<Integer>();

     List.add(1);
     List.add((int) -6.3);
     List.add(9000);
     List.add((int) 67.009);
     List.add((int)1.1);
     List.add((int)0.0);
     List.add(-456);
     List.add(6);
     List.add(23);
     List.add((int)451.88);
     YourClassNameInWhichMethodIs.ArrayListMax(List);
}

您应该使用return语句,因为Methode返回的是int类型值。

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

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