在以下代码中,永远不会执行for循环。我试图用断点和监视来解决问题。返回的密文长度正确,但是for循环直到int i >= ciphertext.length()
才增加。实际上,似乎在“调试”消息之后什么也没有执行。
private void decrypt_btnActionPerformed(java.awt.event.ActionEvent evt) {
String alphabet= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
String ciphertext="fRcXFBxXTRJ";
status_label.setText( "Decryption has begun" );
JOptionPane.showMessageDialog(null,"ciphertext-length: " + ciphertext.length() + "\n" + ciphertext,"Debug", JOptionPane.INFORMATION_MESSAGE);
for (int i = 0; i>=ciphertext.length(); i--){
System.out.println("inc:" + i);
String cipher_current_char = getLetterAtIndex(ciphertext, i);
int pos_char_in_alphabet = getIndexAtLetter(alphabet, cipher_current_char);
if(pos_char_in_alphabet-2<0){
plain_ta.setText(getLetterAtIndex(alphabet, (alphabet.length()+(pos_char_in_alphabet -2)+1 ) ));
status_label.setText( 100/i + "%");
}else{
cipher_current_char = getLetterAtIndex(ciphertext, i);
pos_char_in_alphabet = getIndexAtLetter(alphabet, cipher_current_char);
plain_ta.setText(getLetterAtIndex(alphabet, (pos_char_in_alphabet-2)));
status_label.setText( 100/i + "%");
}
}
}
最佳答案
for (int i = ciphertext.length()-1; i>=0; i--){
您需要向后工作。还要注意
-1
,您需要它来避免超出范围的异常(索引从0
开始并转到length -1
)。遇到卡纸时,请始终用纸和铅笔大声圈出它,这总是有帮助的。
关于java - 错误地使用for循环,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14573489/