简体   繁体   English

Java静态方法无法编译

[英]Java static method can't compile

the following messege appears when I compile this code. 当我编译此代码时,将出现以下信息。

ExtractChars(java.lang.String,int) in Question2 cannot be applied to () Question2中的ExtractChars(java.lang.String,int)无法应用于()

What should I fix? 我该怎么解决?

Thanks. 谢谢。

import java.util.Scanner;

public class Question2
{

    public static void main (String[] args)
    {

        ExtractChars();

    }
    public static String ExtractChars(String sOrg, int startPos)
    {

        Scanner scan = new Scanner (System.in);
        String value = "";
        System.out.print("Enter a string: ");
        sOrg = scan.next();
        System.out.print("/nEnter an integer: ");
        startPos = scan.nextInt();
        if (sOrg.length()<=startPos)
        {

            value = "";

        }
        else
        {
            for (int i=startPos; i<sOrg.length();i=i+2)
            {

                value = value + sOrg.charAt(i);

            }
        }

        return value;
    }
}

This is because the function ExtractChars expects two arguments but you are not passing any. 这是因为函数ExtractChars需要两个参数,但是您没有传递任何参数。

EDIT: 编辑:

Looks like you are reading input sOrg and startPos and there is no need to pass them as arguments, you can make them local variables. 看起来您正在读取输入sOrgstartPos并且不需要将它们作为参数传递,可以将它们设置为局部变量。

So change: 所以改变:

public static String ExtractChars(String sOrg, int startPos)
{
 ....

to

public static String ExtractChars()
{
  String sOrg;
  int startPos;
  ....

Also, you are just discarding the return value of the function ExtractChars in main . 同样,您只是在main丢弃函数ExtractChars的返回值。 You might want to print the extracted characters returned by the function (as a string) as: 您可能希望将函数返回的提取字符(作为字符串)打印为:

System.out.println("Extracted Characters = " + ExtractChars());

in your main in place of 在您的主要位置

ExtractChars();

You've defined your method to expect two arguments, but you're really using them as though they were local variables, prompting the user to enter values for them. 您已经将方法定义为期望有两个参数,但是您实际上是在使用它们,就像它们是局部变量一样,提示用户输入它们的值。

You should probably rewrite your method something like this 您可能应该像这样重写您的方法

public static String ExtractChars()
{
    String sOrg;
    int startPos;

    Scanner scan = new Scanner (System.in);
    String value = "";
    System.out.print("Enter a string: ");
    sOrg = scan.next();
    System.out.print("/nEnter an integer: ");
    startPos = scan.nextInt();
    if (sOrg.length()<=startPos)
    {

        value = "";

    }
    else
    {
        for (int i=startPos; i<sOrg.length();i=i+2)
        {

            value = value + sOrg.charAt(i);

        }
    }

    return value;
}

so that sOrg and startPos are local variables in your method instead of arguments to it. 因此sOrg和startPos是方法中的局部变量,而不是其参数。

The method call as you have it will then compile. 您将拥有的方法调用将进行编译。

You're also not doing anything with the return value of this function, so it may seem like nothing is happening. 您也不会对该函数的返回值做任何事情,因此似乎没有任何反应。 But this might get you on your way. 但这可能会让您步入正轨。

You have to use arguments when you call ExtractChars. 调用ExtractChars时必须使用参数。 ExtractChars wants a String and an int. ExtractChars需要一个String和一个int。 When you call it, you're passing nothing. 调用它时,您什么也没传递。

its the arguments. 它的论点。 You're calling a function which expects 2 arguments. 您正在调用一个需要2个参数的函数。 The compiler generated message atlest points to that. 编译器生成的消息至少指向该点。

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

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