【问题标题】:Java Comparing char values with set char values in a while loopJava在while循环中将char值与set char值进行比较
【发布时间】:2015-01-14 04:30:43
【问题描述】:

我需要将 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);
 }
 });
 }
 }

【问题讨论】:

    标签: java validation while-loop comparison


    【解决方案1】:

    您可以使用单个正则表达式进行检查,然后只需使用 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 个字母中的一个或多个字母组成的任何输入。当你没有得到匹配时,循环重复。

    【讨论】:

    • 谢谢大卫·华莱士。我对使用正则表达式持谨慎态度,但它们似乎很有用,而且我的书并没有真正向我介绍过它们。
    猜你喜欢
    • 2012-01-07
    • 1970-01-01
    • 2021-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-20
    相关资源
    最近更新 更多