本文介绍了如何使用while循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在此部分代码中输入密码,我正在尝试使用do while循环,但它会进行循环。
I am trying to put a password onto this section of code, I am trying to use a do while loop but it carries on looping.
您能告诉我我的错误在哪里吗?我使用的密码是 1234
。
Can you please show me where my errors are? The password I am using is 1234
.
import javax.swing.*;
public class cipherprac
{
static int choice = 0;
public static String msg;
public static int password = 1234;
public static int response = 0;
public static void main (String[] args)
{
msg = JOptionPane.showInputDialog("Enter the message");
response = Integer.parseInt(JOptionPane.showInputDialog("Enter the password"));
do
{
char enc;
String encmsg = "";
int len = msg.length();
for (int i = 0; i < len; i++)
{
char cur = msg.charAt(i);
int val = (int) cur;
val = val - 30;
enc = (char) val;
encmsg = encmsg + enc;
msg = encmsg;
}
}
while(response == password) ;
JOptionPane.showMessageDialog(null, " " + msg);
}
}
推荐答案
您不要在代码中更改响应
(也不要更改密码
),因此,如果将其设置为 1234
,那么它将永远循环。
You don't change the response
(nor password
) in your code, so if you set it to 1234
then it will be looping for ever.
这篇关于如何使用while循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!