我需要将char值与set 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);
}
});
}
}
答案 0 :(得分: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个字母组成的任何输入。当你没有得到匹配时,循环重复。