我的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.
}
}
当我使用Scanner时,结果还可以。
答案 0 :(得分:3)
在类内部声明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.
}}
答案 1 :(得分:1)
主要问题是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.
}
}
答案 2 :(得分:0)
目前,如果没有扫描程序,则不会将整数设置为mselect
- 因此,如果您要定义一个类似于default
,请选择案例switch(mSelect){
.
.
.
default: JOptionPane.showMessageDialog(null, "ERROR");
}
。
mSelect
另一种解决方案是将默认值设置为int mSelect = 1;
,如
08.13.2015 0:00