简体   繁体   English

我会使用do while循环吗?

[英]Would I just use a do while loop?

I want the user to be able to search this short list but I don't want the JOptionPane to close if a wrong number is entered. 我希望用户能够搜索这个短列表,但如果输入的号码错误,我不希望JOptionPane关闭。 Also how would I set up a terminate word ie if user types "quit" the loop will end and program will exit. 另外我如何设置终止词,即如果用户输入“退出”,循环将结束,程序将退出。 Would I use a do while loop? 我会使用do while循环吗?

  int key, list[] = {8,22,17,24,15};
  int index;

  key = Integer.parseInt(JOptionPane.showInputDialog(null,"Input the integer to find"));

  index = searchList(list, key);

  if(index < list.length)
      JOptionPane.showMessageDialog(null,"The key " + key + " found the element " + index);
      else
      JOptionPane.showMessageDialog(null,"The key " + key + " not found");

     System.exit(0);


}

private static int searchList(int[] n, int k) {
    int i;
    for (i = 0; i < n.length; i++)
        if(n[i] == k)
            break;   //break loop if key found
    return i;

You could try something like this: 你可以尝试这样的事情:

import javax.swing.JOptionPane;


public class Key {

    public static void main(String[] args){

        int key, list[] = {8,22,17,24,15};
        int index;

        String userOption = "null"; 

        while(!userOption.equals("quit")){
            userOption = JOptionPane.showInputDialog(null,"Input the integer to find");

            //Take the cancel action into account
            if(userOption == null){
                break;
            }

            //Try to get valid user input
            try{
                key = Integer.parseInt(userOption);

                index = searchList(list, key);

                if(index < list.length){
                    JOptionPane.showMessageDialog(null,"The key " + key + " found the element " + index);
                    //uncommented the break below to exit from the loop if successful
                    //break;
                }
                else
                    JOptionPane.showMessageDialog(null,"The key " + key + " not found");

            } catch (Exception e){
                //Throw an exception if anything but an integer or quit is entered
                JOptionPane.showMessageDialog(null,"Could not parse int", "Error",JOptionPane.ERROR_MESSAGE);
            }
        }
        System.exit(0);
    }

    private static int searchList(int[] n, int k) {
        int i;
        for (i = 0; i < n.length; i++)
            if(n[i] == k)
                break;   //break loop if key found
        return i;

    }
}

This isn't perfect code but it should do the job. 这不是完美的代码,但它应该做的工作。 If you have any queries I'd be more than happy to help. 如果您有任何疑问,我将非常乐意为您提供帮助。

Happy Coding, 快乐的编码,

Hayden 海登

I believe the do while loop is ideal for these such menu-IO loops because you need at least one IO. 我相信do while循环非常适合这些菜单IO循环,因为您至少需要一个IO。 Generic pseudocode : 通用伪代码:

Var command;
do {
  command = input();
  switch( command ){
    case opt1 :
      // do something
    case opt2 :
      // do something
    case default :
      // unexpected command
  }
} while( command != "quit" );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM