摆脱for循环

时间:2012-11-18 08:04:00

标签: java netbeans for-loop switch-statement break

我编写了一个程序,用于将字符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循环。

3 个答案:

答案 0 :(得分: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!

答案 1 :(得分: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);
   }
}

答案 2 :(得分: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.
}