简体   繁体   English

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

[英]Java Comparing char values with set char values in a while loop

I need to compare char values with set char values 'g' 'c' 'a' 't'(lower and upper case), for i want only those values to be entered. 我需要将char值与设置的char值'g''c''a''t'(小写和大写)进行比较,因为我只希望输入那些值。 I can not seem to get certain cases of my input validation working. 我似乎无法获得输入验证工作的某些情况。

f in the below strings can stand for any length of string that is not characters g,c,a,t. 以下字符串中的f可以代表任何非字符串g,c,a,t的字符串。

The string "fffffff" keeps in the loop. 字符串“ fffffff”保持循环。 The string "fgf" keeps in the loop. 字符串“ fgf”保持循环。

However, i want the strings, "fffffg" or "gfg" to exit the loop, and they are not doing so. 但是,我希望字符串“ fffffg”或“ gfg”退出循环,而他们没有这样做。

The actual purpose of the exercise, to take a user input of nucleotides like g,c,a,t like the one's in DNA, and convert them into the complementary string of RNA. 该练习的实际目的是让用户输入DNA中类似g,c,a,t之类的核苷酸,然后将其转换为互补的RNA串。 G is complement to C and vice versa. G是C的补数,反之亦然。 A is complement to U(the T is replaced with U) and vice versa. A是U的补码(T替换为U),反之亦然。 So if the string is "gcat", the response for RNA should be "cgua". 因此,如果字符串为“ 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);
 }
 });
 }
 }

You could do your check with a single regular expression, and then just use a do/while loop to keep prompting for input until the user enters something valid. 您可以使用单个正则表达式进行检查,然后使用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]+"));

The regular expression will match any input that consists of one or more letters of the 8 shown. 正则表达式将匹配包含所示8个字母中的一个或多个字母的任何输入。 When you don't get a match, the loop repeats. 当您找不到匹配项时,循环将重复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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