嘿,我正在尝试获取GC-字符串的GC-含量是由字符串中C或G符号的百分比给出的。例如,"AGCTATAG“的GC-含量是.375或37.5%。这就是我想出来的。我在计算和返回双精度字符串时遇到了问题。
public static double gcContent(String dna) {
//TODO Implement this method
double gcContent = 0;
double count=0;
for (int i = 0; i < dna.length(); i ++) {
if (gcContent == dna.length()){
gcContent = (dna.length()/ 2) ;
}
return double.toString (gcContent);
}
}发布于 2015-10-16 14:49:42
你的计算没有意义。您必须遍历dna字符串的每个字符,并将其与期望值char进行比较('C‘或'G',大小写?)如果您想以字符串形式返回结果,则还必须将返回类型更改为string。
public static String gcContent(String dna) {
//TODO Implement this method
char c = 'C';
char g = 'G';
double gcContent = 0;
double count=0;
for (int i = 0; i < dna.length(); i ++) {
if (dna.charAt(i) == c || dna.charAt(i) == g){
count++;
}
}
gcContent = count / (double)dna.length();
return String.valueOf(gcContent);
}发布于 2015-10-16 14:25:01
你不能在原始类型变量中调用toString()。您可以使用:
String.valueOf(gcContent)发布于 2015-10-16 14:27:22
有很多方法可以做到这一点。
String.valueOf和Double.toString可以工作,但不能控制格式。
使用数字格式化程序会更强大,因为它允许您控制输出的显示方式。所以你可以把它变成一个小数,或者百分比。
给你读一些东西:
https://docs.oracle.com/javase/tutorial/java/data/numberformat.html http://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html
https://stackoverflow.com/questions/33163789
复制相似问题