import java.util.Scanner;
public class DNA{
public static void main(String [] args) {
Scanner scan = new Scanner(System.in);
System.out.println("What is the dna string?");
StringBuilder DNA = new StringBuilder(scan.nextLine());
DNA.append(scan.nextLine());
for(int i=0;i<DNA.length();i++){
if(DNA.charAt(i)=='A'){
DNA.setCharAt(i,'T');
}
if(DNA.charAt(i)=='T'){
DNA.setCharAt(i,'A');
}
if(DNA.charAt(i)=='G'){
DNA.setCharAt(i,'C');
}
if(DNA.charAt(i)=='C'){
DNA.setCharAt(i,'G');
}
}
System.out.println(DNA);
}
}
所以,这就是我的代码现在。它是一个基本的程序,需要一个DNA链,并给出它的副本。但是,某些字母可以工作,而其他字母则可以。从T到A和C到G的转换工作,但从A到T和从G到C的转换不能。有人可以帮我吗?东西我的代码,我似乎无法弄清楚
您正在使用来自Scanner的输入创建一个StringBuilder,然后再将它附加到StringBuilder。另外一个switch语句会更好 – jthort
使用调试器来遍历代码。应该花费大约两秒时间来发现问题。 –
使用'else if's来防止下一个逆转交换。 – shmosel