我正在尝试为家庭作业完成这个程序,这是唯一不起作用的东西。我在这一行得到了预期的错误:
的System.out.println(myChars.equals(补体));
这是我的代码
public class WCpalindrome {
private static char[] dna = {'A', 'C', 'T', 'G'};
private static ArrayList<Character> myChars = new ArrayList<Character>();
private static ArrayList<Character> reverse = new ArrayList<Character>();
private static ArrayList<Character> complement = new ArrayList<Character>();
public static char toUpperCase(char c) {
if (c == 'A' || c == 'a') {
return 'A';
} else if (c == 'T' || c == 't') {
return 'T';
} else if (c == 'C' || c == 'c') {
return 'C';
}
return 'G';
}
public static char getComplement(char c) {
if (c == 'A' || c == 'a') {
return 'T';
} else if (c == 'T' || c == 't') {
return 'A';
} else if (c == 'C' || c == 'c') {
return 'G';
}
return 'C';
}
public static void main(String[] args) {
char current;
int i = 0;
//Get the input sequence
while (StdIn.hasNextChar()) {
current = StdIn.readChar();
current = toUpperCase(current);
myChars.add(current);
StdOut.println(myChars.get(i));
i++;
}
System.out.println();
//Reverse the sequence
int k = 0;
int size = myChars.size() - 1;
for (int j = size-1; j >= 0; j--) {
current = myChars.get(j);
StdOut.println(current);
reverse.add(k, current);
k++;
}
System.out.println();
//Complement the reversed sequence
int n = 0;
size = myChars.size() - 1;
for (n = 0; n < size; n++) {
current = reverse.get(n);
complement.add(getComplement(current));
StdOut.println(complement.get(n));
}
}
//Prints true if the original equals the complement of the reversal
//else it prints false
System.out.println(myChars.equals(complement));
}
答案 0 :(得分:3)
您正在谈论的线路不在任何方法内。我想你想在主方法中移动它。
答案 1 :(得分:0)
您已将语句System.out.println(...);
置于main()
之外。将它向上移3行。它现在处于预期方法和字段声明的级别。
此外,equals将始终返回false,因为数组实例不会比较内部元素。
One might use
Arrays.equals`但你可能只是使用:
System.out.println(new String(myChars).equals(new String(complement)));
答案 2 :(得分:-1)
System.out.println(myChars.equals(complement));
在主要方法之外。
这应该在最后
}
//Prints true if the original equals the complement of the reversal
//else it prints false
System.out.println(myChars.equals(complement));
}
}