Java 接受char作为参数,但返回int? 公共类booklab 2 { 公共静态void main(字符串[]args) { 阿尔卑斯山脉; 炭流; 整数指数; 扫描仪扫描=新扫描仪(System.in); alph=新BLab2Cons(26); 字符串回答; System.out.println(“给我一个以句号结尾的句子。”); 答案=scan.nextLine(); 系统输出打印长度(alph.长度); 对于(int x=0;x

Java 接受char作为参数,但返回int? 公共类booklab 2 { 公共静态void main(字符串[]args) { 阿尔卑斯山脉; 炭流; 整数指数; 扫描仪扫描=新扫描仪(System.in); alph=新BLab2Cons(26); 字符串回答; System.out.println(“给我一个以句号结尾的句子。”); 答案=scan.nextLine(); 系统输出打印长度(alph.长度); 对于(int x=0;x,java,Java,这是我制作方法的类,如果您对其进行测试,convertIndex将无法工作。我已经尝试了将近一个小时,试图让它接受一个字符作为参数,但返回一个int。我不是一个非常先进的编码器,所以保持它的简单将是非常必要的。我曾尝试将字符转换为int,这似乎很有效,但当我需要在主代码中实际使用该方法时,似乎总是必须使用字符串作为参数,而不是字符 编辑:这是我得到的错误:将当前的传递到转换索引()将当前的传递到转换索引()尝试以下操作: public class BookLab2 { public st

这是我制作方法的类,如果您对其进行测试,convertIndex将无法工作。我已经尝试了将近一个小时,试图让它接受一个字符作为参数,但返回一个int。我不是一个非常先进的编码器,所以保持它的简单将是非常必要的。我曾尝试将字符转换为int,这似乎很有效,但当我需要在主代码中实际使用该方法时,似乎总是必须使用字符串作为参数,而不是字符


编辑:这是我得到的错误:

将当前的
传递到
转换索引()
将当前的
传递到
转换索引()
尝试以下操作:

public class BookLab2
{
    public static void main (String [] args)
    {
        BLab2Cons alph;
        char current;
        int index;
        Scanner scan = new Scanner(System.in);
        alph = new BLab2Cons(26);
        String answer;
        System.out.println("Give me a sentence that ends with a period.");
        answer = scan.nextLine();
        System.out.println(alph.length);
        for (int x = 0; x < answer.length(); x++)
        {
            current = answer.charAt(x);
            index = answer.convertIndex(x); //This is where I am having problems.
        }
    }
}


public class BLab2Cons 
{
    int index;
    int alph[];
    char test;
    public int length;
    public BLab2Cons(int size)
    {
        alph = new int[size];
        length = alph.length;
    }
    public int convertIndex(char x) //The method that is not working.
    {
        index = (int)x - (int)'a';
        return index;
    }
}
试试这个:

public class BookLab2
{
    public static void main (String [] args)
    {
        BLab2Cons alph;
        char current;
        int index;
        Scanner scan = new Scanner(System.in);
        alph = new BLab2Cons(26);
        String answer;
        System.out.println("Give me a sentence that ends with a period.");
        answer = scan.nextLine();
        System.out.println(alph.length);
        for (int x = 0; x < answer.length(); x++)
        {
            current = answer.charAt(x);
            index = answer.convertIndex(x); //This is where I am having problems.
        }
    }
}


public class BLab2Cons 
{
    int index;
    int alph[];
    char test;
    public int length;
    public BLab2Cons(int size)
    {
        alph = new int[size];
        length = alph.length;
    }
    public int convertIndex(char x) //The method that is not working.
    {
        index = (int)x - (int)'a';
        return index;
    }
}

问题是您没有正确调用该方法。目前,你有

 index = alph.convertIndex(current);
但是
convertIndex
是在
BLab2Cons
类中定义的一个实例方法。因此,该方法需要类
BLab2Cons
的实例才能调用它。另外,您正在传入
x
,这是一个
int
,但是您的方法被定义为接受一个
char
参数。因此,简单的解决方法是使用
alph
调用方法

index = answer.convertIndex(x);

问题是您没有正确调用该方法。目前,你有

 index = alph.convertIndex(current);
但是
convertIndex
是在
BLab2Cons
类中定义的一个实例方法。因此,该方法需要类
BLab2Cons
的实例才能调用它。另外,您正在传入
x
,这是一个
int
,但是您的方法被定义为接受一个
char
参数。因此,简单的解决方法是使用
alph
调用方法

index = answer.convertIndex(x);

根据,我认为您可能希望将“x”替换为“当前”

根据,我认为您可能希望将“x”替换为“当前”

使用
alph.convertIndex(当前)
使用
alph.convertIndex(当前)
(;作弊者呵呵,我刚刚将参数从x改为current,因为x是for循环中的整数,该方法调用一个char。错误现在消失了,如果该注释再次被修复/损坏,我将在一秒钟内更新它。谢谢你帮我省去了一个巨大的麻烦哦,我的天gosh@ScaryWombat看来OP从嗨开始就没上过stackoverflow这是最后一句话,但谢谢你,我很感激:)(;作弊者呵呵,我刚刚将参数从x改为current,因为x是for循环中的整数,该方法调用一个char。错误现在消失了,如果该注释再次被修复/损坏,我将在一秒钟内更新它。谢谢你帮我省去了一个巨大的麻烦哦,我的天gosh@ScaryWombat看来OP从嗨开始就没上过stackoverflow这是最后一句话,但谢谢你,我很感激:)