得到一个“方法不适用”的编译错误。

时间:2022-10-19 06:57:26

Can anyone tell my why I am getting an error in line 15 for count(s1)? Below is my code and the exact message I am getting from eclipse:

有人能告诉我为什么我在第15行有一个错误(s1)吗?下面是我从eclipse获得的代码和确切消息:

The method count(String, char) in the type LetterCount is not applicable for the arguments (String)

类型LetterCount中的方法count(String、char)不适用于参数(String)

package count;

import java.util.Scanner;

public class LetterCount {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.print("Enter a string: ");
        String s = input.nextLine();

        System.out.print("Enter a character: ");
        String s1 = input.next();

        System.out.println(s1 + "appears" + count(s1) + "time(s).");

    }
    public static int count(String s, char s1) {
        int count = 0;
        for (int i = 0; i < s.length(); i++)
        {
            if (s.charAt(i) == s1)
            {
                count++;
            }
        }
        return count;
    }
}

4 个解决方案

#1


4  

Your method declaration expects two parameters, you're only passing one when you call it.

您的方法声明需要两个参数,当您调用它时,您只传递一个参数。

You probably want to pass the variable "s" as well.

你可能想要传递变量s。

#2


2  

You only passed one argument to count(Sring s, char s1)

你只通过一个参数来计数(Sring, char s1)

Easy mistake to make.

容易犯的错误。

#3


2  

Modify this

修改这个

System.out.println(s1 + "appears" + count(s1, someCharVariable) + "time(s).");

It requires 2 arguments and you are supplying only one.

它需要两个参数,而你只提供一个参数。

It's the only error. I have copied your code and tested accordingly.

这是唯一的错误。我已经复制了您的代码并进行了相应的测试。


Try this. you will have to apply some condition to ensure that what is inputted through the statement String s1 = input.next(); is a valid character in Java.

试试这个。您将不得不应用一些条件来确保输入的语句字符串s1 = input.next();是Java中的一个有效字符。

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    System.out.print("Enter a string: ");
    String s = input.nextLine();

    System.out.print("Enter a character: ");
    String s1 = input.next();

    System.out.println(s1 + " appears " + count(s, s1.charAt(0)) + " time(s).");
}

public static int count(String s, char s1) {
    int count = 0;
    for (int i = 0; i < s.length(); i++)
    {
        if (s.charAt(i) == s1)
        {
            count++;
        }
    }
    return count;
}

#4


1  

System.out.println(s1 + "appears" + count(s1) + "time(s).");

system . out。println(s1 +“出现”+ count(s1) +“time(s)”);

should be:

应该是:

System.out.println(s1 + "appears" + count(s,s1) + "time(s).");

system . out。println(s1 +“出现”+计数(年代,s1)+“时间(s)。”);

#1


4  

Your method declaration expects two parameters, you're only passing one when you call it.

您的方法声明需要两个参数,当您调用它时,您只传递一个参数。

You probably want to pass the variable "s" as well.

你可能想要传递变量s。

#2


2  

You only passed one argument to count(Sring s, char s1)

你只通过一个参数来计数(Sring, char s1)

Easy mistake to make.

容易犯的错误。

#3


2  

Modify this

修改这个

System.out.println(s1 + "appears" + count(s1, someCharVariable) + "time(s).");

It requires 2 arguments and you are supplying only one.

它需要两个参数,而你只提供一个参数。

It's the only error. I have copied your code and tested accordingly.

这是唯一的错误。我已经复制了您的代码并进行了相应的测试。


Try this. you will have to apply some condition to ensure that what is inputted through the statement String s1 = input.next(); is a valid character in Java.

试试这个。您将不得不应用一些条件来确保输入的语句字符串s1 = input.next();是Java中的一个有效字符。

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    System.out.print("Enter a string: ");
    String s = input.nextLine();

    System.out.print("Enter a character: ");
    String s1 = input.next();

    System.out.println(s1 + " appears " + count(s, s1.charAt(0)) + " time(s).");
}

public static int count(String s, char s1) {
    int count = 0;
    for (int i = 0; i < s.length(); i++)
    {
        if (s.charAt(i) == s1)
        {
            count++;
        }
    }
    return count;
}

#4


1  

System.out.println(s1 + "appears" + count(s1) + "time(s).");

system . out。println(s1 +“出现”+ count(s1) +“time(s)”);

should be:

应该是:

System.out.println(s1 + "appears" + count(s,s1) + "time(s).");

system . out。println(s1 +“出现”+计数(年代,s1)+“时间(s)。”);