Java 在 while 循环中比较 char 值与设置的 char 值

Java Comparing char values with set char values in a while loop

我需要将 char 值与设置的 char 值进行比较 'g' 'c' 'a' 't'(小写和大写),因为我只希望这些值是进入。我似乎无法在某些情况下进行输入验证。

下面字符串中的f可以代表g,c,a,t之外的任意长度的字符串

字符串"fffffff"一直在循环中。 字符串 "fgf" 保持在循环中。

但是,我希望字符串 "fffffg" 或 "gfg" 退出循环,但它们没有这样做。

该练习的实际目的是,将用户输入的核苷酸(如 g、c、a、t)像 DNA 中的核苷酸一样,并将它们转换成互补的 RNA 串。 G 与 C 互补,反之亦然。 A 是 U 的补充(T 被 U 代替),反之亦然。 所以如果字符串是 "gcat",RNA 的响应应该是 "cgua".

import java.text.DecimalFormat;
import javax.swing.SwingUtilities;
import javax.swing.JOptionPane;
import java.util.Random;

//getting my feet wet, 1/13/2015, program is to take a strand of nucleotides, G C  A   T, for DNA and give
//the complementary RNA strand, C G  U A.
public class practiceSixty {

public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable() {
     public void run() {

        String input = null;

        boolean loopControl = true;

        char nucleotide;

        while(loopControl == true)
        {
            input = JOptionPane.showInputDialog(null, " Enter the sequence of nucleotides(G,C,A and T) for DNA, no spaces ");
            for(int i = 0; i < input.length(); i++)
            {
            nucleotide = input.charAt(i);

                if(!(nucleotide == 'G' || nucleotide == 'g' || nucleotide == 'C' || nucleotide == 'c' || nucleotide == 'A' || nucleotide == 'a' || nucleotide == 'T' || nucleotide == 't' ))
                {
                 loopControl = true;
                }
                else if(nucleotide == 'G' || nucleotide == 'g' || nucleotide == 'C' || nucleotide == 'c' || nucleotide == 'A' || nucleotide == 'a' || nucleotide == 'T' || nucleotide == 't' )
                {
                loopControl = false;
                System.out.println(nucleotide);
                }
            }

         }
            JOptionPane.showMessageDialog(null, "the data you entered is " + input);

            StringBuilder dna = new StringBuilder(input);

            for(int i = 0; i < input.length(); i++)
            {
            nucleotide = input.charAt(i);

                if(nucleotide == 'G' || nucleotide == 'g' )
                {
                    dna.setCharAt(i, 'c');
                }
                else if( nucleotide == 'C' || nucleotide == 'c')
                {
                    dna.setCharAt(i, 'g');
                }
                if(nucleotide == 'A' || nucleotide == 'a')
                {
                    dna.setCharAt(i, 'u');
                }
                else if(nucleotide == 'T' || nucleotide == 't')
                {
                    dna.setCharAt(i, 'a');
                }
            }
             JOptionPane.showMessageDialog(null, "the DNA is  , " + input + "  the RNA is  " + dna);
 }
 });
 }
 }

您可以使用单个正则表达式进行检查,然后只需使用 do/while 循环来不断提示输入,直到用户输入有效内容。

do {
    input = JOptionPane.showInputDialog(
        null, " Enter the sequence of nucleotides(G,C,A and T) for DNA, no spaces ");
} while (!input.matches("[GCATgcat]+"));

正则表达式将匹配由所示 8 中的一个或多个字母组成的任何输入。当你没有得到匹配时,循环重复。