1

我编写了一个程序来将字符 a、g、t、c 从文本框转换为另一个值问题是,当除了 a、g、t、c 之外的多个值时,它会为每个无效字符显示一条错误消息。我怎么能这样做以便在它看到的第一个无效字符上停止转换方法。下面是我的代码。谢谢

private void dna2rna() {
   String DNA = dna_ta.getText();
   char[]readdna;
   readdna=DNA.toCharArray();
   for (int x=0;x<readdna.length;x++){
      switch(DNA.charAt(x))
      {
         case 'A': readdna[x] = 'U';break;
         case 'T': readdna[x] = 'A';break;
         case 'C': readdna[x] = 'G';break;
         case 'G': readdna[x] = 'C';break;
         case 'a': readdna[x] = 'u';break;
         case 't': readdna[x] = 'a';break;
         case 'c': readdna[x] = 'g';break;
         case 'g':readdna[x] = 'c';break;
         default:
            JOptionPane.showMessageDialog(frame,
               "Not a DNA Sequence.Please Retry",
               "Sequence error",
               JOptionPane.ERROR_MESSAGE);
            dna_ta.setText("");
            rna_ta.setText(""); 
            break;
      }
      String rna= new String(readdna);
      rna_ta.setText(rna);
   }
}

问题是如果它找到了默认情况,它就会退出并继续 for 循环。如何防止它通过 for 循环。

4

3 回答 3

2

您可以将default选项更改为,return而不是break. 你也可以让它抛出一个IllegalArgumentException(或你喜欢的任何其他异常)然后在调用方法中处理它:

default:
    throw new IllegalArgumentException("Not a DNA Sequence");

然后在调用者中:

try {
    dna2rna();
} catch (IllegalArgumentException e) {
    JOptionPane.showMessageDialog(frame,"Not a DNA Sequence.Please   Retry","Sequence error",JOptionPane.ERROR_MESSAGE);
    dna_ta.setText("");
    rna_ta.setText("");
    // Whatever else you were doing when error==true.
}
于 2012-11-18T08:41:46.007 回答
2

向循环添加标签,使用 break 和这个标签。一种goto风格,但它有效......

private void dna2rna() {
    String DNA = dna_ta.getText();
    char[]readdna;
    readdna=DNA.toCharArray();
    forLoop: // <----------- label
    for (int x=0;x<readdna.length;x++){

         switch(DNA.charAt(x))
         {
             case 'A': readdna[x] = 'U';break;
             case 'T': readdna[x] = 'A';break;
             case 'C': readdna[x] = 'G';break;
             case 'G': readdna[x] = 'C';break;
             case 'a': readdna[x] = 'u';break;
             case 't': readdna[x] = 'a';break;
             case 'c': readdna[x] = 'g';break;
             case 'g':readdna[x] = 'c';break;
             default:
                 JOptionPane.showMessageDialog(frame,"Not a DNA Sequence.Please   Retry","Sequence error",JOptionPane.ERROR_MESSAGE);
                dna_ta.setText("");
                 rna_ta.setText(""); break forLoop; // <----------- break to label
         }
         String rna= new String(readdna);
         rna_ta.setText(rna);
    }

}

简单的测试方法:

private static void dna2rna() {
    char[] readdna = { 'A', 'T', 'W', 'C', 'T', };
    forLoop: for (int x = 0; x < readdna.length; x++) {

        System.out.println("reading: " + readdna[x]);
        switch (readdna[x]) {
             case 'A': readdna[x] = 'U';break;
             case 'T': readdna[x] = 'A';break;
             case 'C': readdna[x] = 'G';break;
        default:
            System.out.println("some weird value!");
            break forLoop;
        }
    }

}

输出:

reading: A
reading: T
reading: W
some weird value! breaking!
于 2012-11-18T08:06:10.387 回答
2

您可以使用标志来指示for循环退出:

private void dna2rna() {
   String DNA = dna_ta.getText();
   char[]readdna;
   readdna=DNA.toCharArray();
   boolean error = false;
   for (int x=0;x<readdna.length && !error;x++){

      switch(DNA.charAt(x))
      {
        case 'A': readdna[x] = 'U';break;
        case 'T': readdna[x] = 'A';break;
        case 'C': readdna[x] = 'G';break;
        case 'G': readdna[x] = 'C';break;
        case 'a': readdna[x] = 'u';break;
        case 't': readdna[x] = 'a';break;
        case 'c': readdna[x] = 'g';break;
        case 'g':readdna[x] = 'c';break;
        default:
            JOptionPane.showMessageDialog(frame,"Not a DNA Sequence.Please   Retry","Sequence error",JOptionPane.ERROR_MESSAGE);
            dna_ta.setText("");
            rna_ta.setText("");
            error = true;
            break; // or possibly: continue;
      }
      String rna= new String(readdna);
      rna_ta.setText(rna);
   }
}
于 2012-11-18T08:16:14.137 回答