英文:
JFrame freezes up when I press a button
问题
private void btnEncryptActionPerformed(java.awt.event.ActionEvent evt) {
String origMessage = txtDInput.getText();
String encMessage = "";
String revMessage = "";
String extraStg1 = "";
String extraStg2 = "";
char tempChar;
char tempExtraChar;
int tempAscii;
int tempExtraAscii;
for (int i = origMessage.length() - 1; i >= 0; i--) //reverses message
{
revMessage = revMessage + origMessage.charAt(i);
}
for (int i = 0; i < revMessage.length(); i++)
{
tempChar = revMessage.charAt(i); //stores character in the tempChar variable
tempAscii = (int)tempChar; //converts the character into an Ascii value
tempAscii = tempAscii + 3; //adds 3 to Ascii value
tempChar = (char)tempAscii; //converts Ascii value back into a character value
encMessage = encMessage + tempChar; //adds the new character to the encrypted string and repeats for every character
}
for (int i = 0; i <= 7; i++)
{
tempExtraAscii = (int)Math.round(Math.random()*25+1+96); //generates a random integer between 97 and 122
tempExtraChar = (char)tempExtraAscii; //convert the random integer into a character
extraStg1 = extraStg1 + tempExtraChar; //add the extra character to tempExtraStg1
}
for (int i = 0; i <= 7; i++)
{
tempExtraAscii = (int)Math.round(Math.random()*25+1+96); //generates a random integer between 97 and 122
tempExtraChar = (char)tempExtraAscii; //convert the random integer into a character
extraStg2 = extraStg2 + tempExtraChar; //add the extra character to tempExtraStg2
}
encMessage = extraStg1 + encMessage + extraStg2;
txtEncrypted.setText(encMessage);
}
英文:
I'm making an encryption program and for some reason the program completely freezes up when I press the button. I'm not sure what's wrong because I've made many simple GUIs before and I've never encountered this is issue. Here's the void for the button:
private void btnEncryptActionPerformed(java.awt.event.ActionEvent evt) {
String origMessage = txtDInput.getText();
String encMessage = "";
String revMessage = "";
String extraStg1 = "";
String extraStg2 = "";
char tempChar;
char tempExtraChar;
int tempAscii;
int tempExtraAscii;
for (int i = origMessage.length() - 1; i >= 0; i = i--) //reverses message
{
revMessage = revMessage + origMessage.charAt(i);
}
for (int i = 0; i < revMessage.length(); i = i++)
{
tempChar = revMessage.charAt(i); //stores character in the tempChar variable
tempAscii = (int)tempChar; //converts the character into an Ascii value
tempAscii = tempAscii + 3; //adds 3 to Ascii value
tempChar = (char)tempAscii; //converts Ascii value back into a character value
encMessage = encMessage + tempChar; //adds the new character to the encrypted string and repeats for every character
}
for (int i = 0; i <= 7; i++)
{
tempExtraAscii = (int)Math.round(Math.random()*25+1+96); //generates a random integer between 97 and 122
tempExtraChar = (char)tempExtraAscii; //convert the random integer into a character
extraStg1 = extraStg1 + tempExtraChar; //add the extra character to tempExtraStg1
}
for (int i = 0; i <= 7; i++)
{
tempExtraAscii = (int)Math.round(Math.random()*25+1+96); //generates a random integer between 97 and 122
tempExtraChar = (char)tempExtraAscii; //convert the random integer into a character
extraStg2 = extraStg2 + tempExtraChar; //add the extra character to tempExtraStg2
}
encMessage = extraStg1 + encMessage + extraStg2;
txtEncrypted.setText(encMessage);
}
I'm a beginner at this so I'd appreciate it if the answer are as simple as possible. Thanks.
答案1
得分: 2
这是问题:
for (int i = 0; i < revMessage.length(); i = i++)
i = i++
是一个空操作 - 它增加了 i
,但随后又将其设置回原始值,因此您的循环将永远执行。只需将其更改为:
for (int i = 0; i < revMessage.length(); i++)
实际上您之前也有相同的问题:
for (int i = origMessage.length() - 1; i >= 0; i = i--)
应改为:
for (int i = origMessage.length() - 1; i >= 0; i--)
(顺便说一句,这并不是以有用的方式进行的“加密”,而且无论如何您都不应自己实现加密,但我已按您的要求回答了这个问题。)
英文:
This is the problem:
for (int i = 0; i < revMessage.length(); i = i++)
The i = i++
is a no-op - it increments i
, but then sets it back to the original value, so your loop will execute forever. Just change that to:
for (int i = 0; i < revMessage.length(); i++)
You actually have the same problem earlier:
for (int i = origMessage.length() - 1; i >= 0; i = i--)
should be
for (int i = origMessage.length() - 1; i >= 0; i--)
(As a side note, this isn't really "encryption" in a useful way, and you shouldn't roll your own encryption anyway, but I've addressed the question as asked.)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论