Tim*_* S. 0 java string loops for-loop
在以下代码中,永远不会执行for循环.我试图用断点和手表解决问题.返回密文的正确长度,但for循环直到没有增加int i >= ciphertext.length()
.事实上,似乎没有任何东西执行过'Debug'消息.
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 + "%");
}
}
}
Run Code Online (Sandbox Code Playgroud)
for (int i = ciphertext.length()-1; i>=0; i--){
Run Code Online (Sandbox Code Playgroud)
你需要倒退.还要注意-1
,你需要它来避免超出范围的异常(索引开始于0
和转到length -1
).
当你被困住时,总是用纸和铅笔大声追踪你的循环,这总是有帮助的.