How to use do while loops [closed]

ⅰ亾dé卋堺 提交于 2019-12-12 02:32:07

问题


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.

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);
  }
}

回答1:


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.




回答2:


response = Integer.parseInt(JOptionPane.showInputDialog("Enter the password")); is the last time you set "response", so it will be that forever, hence the infinite loop.

This seems to be some sort of custom encryption? Anyway, you'd want to do something like:

do
{
    ...
} 
while(response == password && "Certain condition isn't met");

This way it will end the loop if for some reason the user input changes OR if it is done with your process.




回答3:


Well you do not change the value of your password or response variable inside the loop. So how is the loop supposed to stop?

Your loop will run once and at the end check whether condition for continuing is met. If yes, it will start the next iteration of the loop code block. If you do not change anything inside the loop which has an effect on the loop's condition, it will run once (if the condition evaluates to false) or until you kill the program (if the condition evaluates to true).

As for the normal work of a loop, here you can find a good explanation with examples in many languages (including Java): Wikipedia link




回答4:


There are multiple issues:

  • Not updating response inside the loop
  • While condition loops again if successful, I think it should try again on unsuccessful password if I understand correctly.

Do you want to keep trying until your password is correct?

In this case, your while condition should be true when they are not equal, so that if it is not successful, you keep trying.

In your case, if your response is not 1234, it will not try, and if it is, it will loop forever.

Ideally for a do while loop:

set condition value before loop
do{
    // do some work
    // update condition value
}while(condition);


来源:https://stackoverflow.com/questions/19681013/how-to-use-do-while-loops

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!