简体   繁体   中英

Why doesn't my code listen?

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");
       }
   }
}

Essentially I need the user to input two DNA sequences, and then the program needs to compare them and determine if the two sequences are reverse compliments of each other. I'm not looking for someone to complete this assignment for me, I'm just at a loss as to why it doesn't work properly. When I test it it always says that the two are reverse compliments.

screenshot of assignment

You can just compare seqSecond with seqReverseCompliment using String.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");
       }
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM