【问题标题】:method cannot be applied to given types error in java方法不能应用于java中的给定类型错误
【发布时间】:2014-11-06 02:15:23
【问题描述】:
import java.util.Scanner;

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

            System.out.println("Input message to be coded: ");
            input = keyboard.nextLine();
            System.out.println("Input: " + input);

            for (int i = 0; i < input.length(); i++)
            {
                    scramble += scrambleMessage(input.charAt(i));
            }

            System.out.println("Coded message is: " + scramble);

   }

public static char scrambleMessage( char c, int x)
   {
            int charc = c;

            if (!Character.isLetter(c))
                    return c;

            charc = (charc * (charc - 1))/(charc + (charc - 1)) + 5;

            return (char)charc;
   }
}

当我尝试编译这段代码时,它会说:

: error: method scrambleMessage in class main cannot be applied to given types;
scramble += scrambleMessage(input.charAt(i));
            ^     

我不明白它在说什么是错误的。该程序的目的是获取在 main 中输入的字符串并通过 scrambleMessage() 函数运行它以更改字母。我通过逐个字符地运行字符串来运行它,使每个字符都是相应的数字,然后将其重新设置为字符。不知道发生了什么给我这个错误。

【问题讨论】:

    标签: java function methods compiler-errors


    【解决方案1】:

    错误说你: The method scrambleMessage(char, int) in the type main is not applicable for the arguments (char) , 您需要传递 char 和 int ,您只需传递一个参数。 或编辑

    public static char scrambleMessage( char c, int x)
    

    public static char scrambleMessage( char c)
    

    我认为你还有这样的错误:

    The local variable scramble may not have been initialized
    

    你必须改变这一行:

    String input, scramble ;
    

    String input, scramble = "";
    

    【讨论】:

      【解决方案2】:

      因为你的 scrambleMessage(char c, int x) 有 2 个参数。你应该传递一个 char 和 int 。因为你没有使用 int 参数删除它

      public static char scrambleMessage( char c)
         {
                  int charc = c;
      
                  if (!Character.isLetter(c))
                          return c;
      
                  charc = (charc * (charc - 1))/(charc + (charc - 1)) + 5;
      
                  return (char)charc;
         }
      

      你也应该改变这一行

      String input, scramble =null;
      

      String input, scramble = "";
      

      因为局部变量在使用前应该被初始化并且你应该避免为空

      【讨论】:

      • 所以我修复了它并编译了它,但现在无论我在第一个字符中输入什么字符串总是“null”。它是如何计算的?
      • 更改字符串输入,scramble =null;到字符串输入,scramble = "";
      猜你喜欢
      • 2014-03-01
      • 2019-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-21
      相关资源
      最近更新 更多