为什么我的代码不听?

时间:2016-01-25 03:33:11

标签: java dna-sequence

import java.util.Scanner;

public class ReverseCompliment 
{
   public static void main(String[] args)
   {
      Scanner in = new Scanner(System.in);
      String seqFirst = "0";
      String seqSecond = "0";
      String seqReverseCompliment = "";
      boolean isOrIsNot = true;

      System.out.print("Enter the first sequence: ");
      seqFirst = in.next();
      System.out.print("Enter the second sequence: ");
      seqSecond = in.next();

      for ( int i = 0; i < seqFirst.length(); i++)
      {
         if ( seqFirst.charAt(i) == 'A')
         {
            seqReverseCompliment = "T" + seqReverseCompliment;
         }
         else if ( seqFirst.charAt(i) == 'T')
         {
            seqReverseCompliment = "A" + seqReverseCompliment;
         }
         else if ( seqFirst.charAt(i) == 'C')
         {
            seqReverseCompliment = "G" + seqReverseCompliment;
         }
         else if ( seqFirst.charAt(i) == 'G')
         {
            seqReverseCompliment = "C" + seqReverseCompliment;
         }
         else
         {
            System.out.println("incorrect input");
         }
      }
       for ( int i = 0; i < seqFirst.length(); i++)
       {
          if ( seqSecond.charAt(i) != seqReverseCompliment.charAt(i) )
          {
             isOrIsNot = false;
          }
          else if ( seqSecond.charAt(i) == seqReverseCompliment.charAt(i) )
          {
             isOrIsNot = true;
          }
       }
       if ( isOrIsNot = true )
       {
          System.out.println("These are reverse compliments");
       }
       else if (
       {
          System.out.println("These are not reverse compliments");
       }
   }
}

基本上我需要用户输入两个DNA序列,然后程序需要比较它们并确定这两个序列是否是彼此的反向互补。我没有找人帮我完成这项任务,我只是不知道为什么它不能正常工作。 当我测试时,它总是说这两个是相反的赞美。

screenshot of assignment

1 个答案:

答案 0 :(得分:0)

您可以使用seqSecond

seqReverseComplimentString.equals进行比较
import java.util.Scanner;

public class ReverseCompliment 
{
   public static void main(String[] args)
   {
      Scanner in = new Scanner(System.in);
      String seqFirst = "0";
      String seqSecond = "0";
      String seqReverseCompliment = "";

      System.out.print("Enter the first sequence: ");
      seqFirst = in.next();
      System.out.print("Enter the second sequence: ");
      seqSecond = in.next();

      for ( int i = 0; i < seqFirst.length(); i++)
      {
         if ( seqFirst.charAt(i) == 'A')
         {
            seqReverseCompliment = "T" + seqReverseCompliment;
         }
         else if ( seqFirst.charAt(i) == 'T')
         {
            seqReverseCompliment = "A" + seqReverseCompliment;
         }
         else if ( seqFirst.charAt(i) == 'C')
         {
            seqReverseCompliment = "G" + seqReverseCompliment;
         }
         else if ( seqFirst.charAt(i) == 'G')
         {
            seqReverseCompliment = "C" + seqReverseCompliment;
         }
         else
         {
            System.out.println("incorrect input");
         }
      }
       if ( seqSecond.equals(seqReverseCompliment) )
       {
          System.out.println("These are reverse compliments");
       }
       else if (
       {
          System.out.println("These are not reverse compliments");
       }
   }
}