Joptionpane 不处理 switch-case
Joptionpane does not process switch-case
我的 switch-case 选项和 joptionpane 有问题。选择四个选项之一后,程序结束。它不显示选择。它应该显示 choice/selection.
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
- 所以如果您要定义这样的一个,则您的开关选择大小写 default
:
switch(mSelect){
.
.
.
default: JOptionPane.showMessageDialog(null, "ERROR");
}
另一种解决方案是将默认值设置为 mSelect
,如
int mSelect = 1;
在 class 而非方法内声明 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
return value/option 并将其包含在 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.
}
}
我的 switch-case 选项和 joptionpane 有问题。选择四个选项之一后,程序结束。它不显示选择。它应该显示 choice/selection.
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
- 所以如果您要定义这样的一个,则您的开关选择大小写 default
:
switch(mSelect){
.
.
.
default: JOptionPane.showMessageDialog(null, "ERROR");
}
另一种解决方案是将默认值设置为 mSelect
,如
int mSelect = 1;
在 class 而非方法内声明 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
return value/option 并将其包含在 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.
}
}