繁体   English   中英

Joptionpane不处理开关盒

[英]Joptionpane does not process switch-case

我的开关盒选项和joptionpane遇到问题。 选择四个选项之一后,程序结束。 它不显示选择。 它应该显示选择/选择。

  import javax.swing.JOptionPane; // JOptionPane call

  public class OnlineStore 
  {
     // public static void main(String[] args) // main program
      public void display_menu() // Not the main program but the main menu.
      {
          String main_selection;
          int mSelect;

          main_selection = JOptionPane.showInputDialog("Welcome!\n\n1. Add T-                Shirt Order\n2. Edit T-Shirt Order\n3. View Current Order\n4. Checkout\n\nPlease   enter your choice: ");
    mSelect = Integer.parseInt(main_selection);

      }

      public OnlineStore() // Switch-case program
      {
          display_menu(); // Call display menu.
          switch (mSelect)
          {
              case 1:
                  JOptionPane.showMessageDialog(null, "Option 1");
                  break;
              case 2:
                  JOptionPane.showMessageDialog(null, "Option 2");
                  break;
              case 3:
                  JOptionPane.showMessageDialog(null, "Option 3");
                  break;
              case 4:  // Deliberately not including a default selection.
                  JOptionPane.showMessageDialog(null, "Option 4");
                  break;  
           } 

      } 

      public static void main(String[] args) // main program
      {
          new OnlineStore(); // Call out the program.
      }
  }

当我使用扫描仪时,结果还可以。

在类内部而不是方法内部声明mSelect变量。

public class OnlineStore {
int mSelect;
// public static void main(String[] args) // main program

public void display_menu() // Not the main program but the main menu.
{
    String main_selection;

    main_selection = JOptionPane.showInputDialog("Welcome!\n\n1. Add T-                Shirt Order\n2. Edit T-Shirt Order\n3. View Current Order\n4. Checkout\n\nPlease   enter your choice: ");
    mSelect = Integer.parseInt(main_selection);

}

public OnlineStore() // Switch-case program
{
    display_menu(); // Call display menu.
    switch (mSelect) {
        case 1:
            JOptionPane.showMessageDialog(null, "Option 1");
            break;
        case 2:
            JOptionPane.showMessageDialog(null, "Option 2");
            break;
        case 3:
            JOptionPane.showMessageDialog(null, "Option 3");
            break;
        case 4:  // Deliberately not including a default selection.
            JOptionPane.showMessageDialog(null, "Option 4");
            break;
    }

}

public static void main(String[] args) // main program
{
    new OnlineStore(); // Call out the program.
}}

主要问题是display_menu发生的事情与OnlineStore的处理之间没有上下文关系。

相反,为什么不让display_menu返回所选的值/选项并将其包含在switch语句中,例如

public class OnlineStore {

    // public static void main(String[] args) // main program

    public int display_menu() // Not the main program but the main menu.
    {
        String main_selection;

        main_selection = JOptionPane.showInputDialog("Welcome!\n\n1. Add T-                Shirt Order\n2. Edit T-Shirt Order\n3. View Current Order\n4. Checkout\n\nPlease   enter your choice: ");
        return Integer.parseInt(main_selection);

    }

    public OnlineStore() // Switch-case program
    {
        switch (display_menu()) {
            case 1:
                JOptionPane.showMessageDialog(null, "Option 1");
                break;
            case 2:
                JOptionPane.showMessageDialog(null, "Option 2");
                break;
            case 3:
                JOptionPane.showMessageDialog(null, "Option 3");
                break;
            case 4:  // Deliberately not including a default selection.
                JOptionPane.showMessageDialog(null, "Option 4");
                break;
        }

    }

    public static void main(String[] args) // main program
    {
        new OnlineStore(); // Call out the program.
    }
}

循环菜单

public class OnlineStore {

    // public static void main(String[] args) // main program

    public int display_menu() // Not the main program but the main menu.
    {
        String main_selection;

        main_selection = JOptionPane.showInputDialog("Welcome!\n\n1. Add T-                Shirt Order\n2. Edit T-Shirt Order\n3. View Current Order\n4. Checkout\n\nPlease   enter your choice: ");
        return Integer.parseInt(main_selection);

    }

    public OnlineStore() // Switch-case program
    {
        boolean exit = false;
        do {
            switch (display_menu()) {
                case 1:
                    JOptionPane.showMessageDialog(null, "Option 1");
                    break;
                case 2:
                    JOptionPane.showMessageDialog(null, "Option 2");
                    break;
                case 3:
                    JOptionPane.showMessageDialog(null, "Option 3");
                    break;
                case 4:  // Deliberately not including a default selection.
                    JOptionPane.showMessageDialog(null, "Option 4");
                    break;
            }
        } while (!exit);
    }

    public static void main(String[] args) // main program
    {
        new OnlineStore(); // Call out the program.
    }
}

目前,没有Scanner,没有将整数设置为mselect因此,如果您定义如下所示,则开关选择default大小写:

switch(mSelect){
.
.
.
default: JOptionPane.showMessageDialog(null, "ERROR");
}

另一种解决方案是将默认值设置为mSelect例如

int mSelect = 1;

暂无
暂无

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

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