方法不能应用于java中的给定类型错误

时间:2014-11-06 02:15:24

标签: java function methods compiler-errors

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()函数运行它以更改字母。我通过在char by char基础上运行字符串来运行它,使每个char成为相应的数字,然后将其重新设置为char。不知道发生了什么事就是给我这个错误。

2 个答案:

答案 0 :(得分: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 = "";

答案 1 :(得分:0)

因为你的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