必须为数组类型,但解析为字符串 [英] Must be an array type but resolved to string

查看:96
本文介绍了必须为数组类型,但解析为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码中出现必须为数组类型,但它解析为字符串"的错误.它还说我(在下面的代码中)无法解析为我没有得到的变量.

I am getting the "Must be an array type but it resolved to string" error in my code. It also says that i (in the code below) cannot be resolved to a variable which I don't get.

    public class DNAcgcount{

        public double ratio(String dna){
        int count=0;
        for (int i=0;i<dna.length();i++);
            if (dna[i]== "c"){
            count+= 1;
            if (dna[i]=="g"){
            count+=1;
        double answer = count/dna.length();
        return answer;

    }

    }


}

}

你们能帮我弄清楚问题出在哪里吗?我是Java编程的新手,因此对这种格式还不完全满意.

Could you guys please help me figure out where the problem lies? I'm new to coding in Java so I am not entirely comfortable with the format yet.

非常感谢, 朱尼德

推荐答案

您不能使用下标(dna[i])访问字符串的字符.改用charAt:

You cannot access a String's character using subscript (dna[i]). Use charAt instead:

dna.charAt(i) == 'c'

此外,"c"String'c'char.

还有一件事情-整数除法(例如int_a / int_b)会得出一个整数,因此您就失去了准确性,反而-将int之一转换为double:

One more thing - integer division ( e.g. int_a / int_b ) results in an int, and so you lose accuracy, instead - cast one of the ints to double:

double answer = count/(double)dna.length();

这篇关于必须为数组类型,但解析为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆