【发布时间】: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