【问题标题】:Java simple for loop does not iterateJava简单的for循环不迭代
【发布时间】:2013-01-21 03:24:41
【问题描述】:

在执行期间,该方法被调用,整数i 在屏幕上显示为0。但是,for 循环内部没有输出,表明 for 循环没有执行。我还用断点对此进行了测试,并获得了相同的结果。任何帮助表示赞赏。

private void decrypt_btnActionPerformed(java.awt.event.ActionEvent evt) {
    int ciphertext_length = Ciphertext().length();
    String decrypted_char = "";
    int i = 0;

    System.out.println("increment" + i);    

    try {
        for (i = 0; i == ciphertext_length; i++) {

            System.out.println("test" + i);

            String cipher_current_char = getLetterAtIndex(Ciphertext(), i);
            int pos_char_in_alphabet = getIndexAtLetter(Alphabet(), cipher_current_char);

            decrypted_char = decrypted_char +
                getLetterAtIndex(Alphabet(), pos_char_in_alphabet - 5);

            status_label.setText(100 / i + "%");
        }
    } catch (Exception e) {
        e.getMessage();
    }

    plain_ta.setText(decrypted_char);
}

【问题讨论】:

  • 您有任何异常吗?你只是在你的 catch 块中忽略了它们,所以现在很难说。

标签: java for-loop loops


【解决方案1】:
  for (i = 0; i==ciphertext_length; i++){

应该是

  for (i = 0; i<ciphertext_length; i++){

【讨论】:

  • 那么你需要再调试一些。 ciphertext_length 的值是多少?
  • String ciphertext="afRcXFBxXTRJ" :可以是任意base64字符的随机字符串
  • ciphertext_length=ciphertext.length();
  • 不,调试器中显示的实际值是多少。我想 Ciphertext() 没有返回您期望的结果。