JFrame在我按下按钮时卡住。

huangapple go评论61阅读模式
英文:

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 = &quot;&quot;;
String revMessage = &quot;&quot;;
String extraStg1 = &quot;&quot;;
String extraStg2 = &quot;&quot;;
char tempChar;
char tempExtraChar;
int tempAscii;
int tempExtraAscii;
for (int i = origMessage.length() - 1; i &gt;= 0; i = i--) //reverses message
{
revMessage = revMessage + origMessage.charAt(i);
}
for (int i = 0; i &lt; 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 &lt;= 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 &lt;= 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 &lt; 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 &lt; revMessage.length(); i++)

You actually have the same problem earlier:

for (int i = origMessage.length() - 1; i &gt;= 0; i = i--)

should be

for (int i = origMessage.length() - 1; i &gt;= 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.)

huangapple
  • 本文由 发表于 2020年9月1日 01:03:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/63675129.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定