@MadProgrammer,switch-case 功能在做出一项选择后停止。我似乎无法让它返回 OnlineStore( )。我尝试了 do-while 语句。它不起作用。无论输入 1 或 2,它都会进入默认状态,然后返回菜单。
import javax.swing.JOptionPane;
public class OnlineStore
{
String[] ColorType = {"blue", "green", "black"};
final int COLOURS = 3; // t choices
int[] Color = new int[COLOURS];
int sum, mselect;
public static int display_menu() // Not the main program but the main menu.
{
String input;
int mselect;
input = JOptionPane.showInputDialog("Welcome!" + "\n\n1. Add tshirt order\n2. Edit tshirt order\n3. View current order\n4. Checkout" + "\n\nPlease enter your choice: ");
// return Integer.parseInt(input);
mselect = Integer.parseInt(input);
return 0;
}
public OnlineStore() // Switch-case program
{
do
{
display_menu();
switch (mselect)
{
case 1:
add_t();
break;
case 2:
edit_t();
break;
default: // If an error is encountered.
JOptionPane.showMessageDialog(null, "Oh dear! Error!");
break;
}
} while (mselect < 3);
}
public final int add_t()
{
for (int index = 0; index < ColorType.length; index++)
{
<output>
}
return Color.length;
}
public final int edit_t()
{
for (int index = 0; index < ColorType.length; index++)
{
<output>
}
public static void main(String[] args) // Main program
{
new OnlineStore(); // Call out the program.
}
}
最佳答案
首先...
public static int display_menu() // Not the main program but the main menu.
{
String input;
int mselect;
input = JOptionPane.showInputDialog("Welcome!" + "\n\n1. Add tshirt order\n2. Edit tshirt order\n3. View current order\n4. Checkout" + "\n\nPlease enter your choice: ");
// return Integer.parseInt(input);
mselect = Integer.parseInt(input);
return 0;
}
您没有返回mselect
,而是返回0
。相反,你可能应该做一些更像......
public static int display_menu() // Not the main program but the main menu.
{
String input;
input = JOptionPane.showInputDialog("Welcome!" + "\n\n1. Add tshirt order\n2. Edit tshirt order\n3. View current order\n4. Checkout" + "\n\nPlease enter your choice: ");
// return Integer.parseInt(input);
return Integer.parseInt(input);
}
接下来,您将使用 mselect
,它没有任何值(value)来检查您的 switch
public OnlineStore() // Switch-case program
{
do {
display_menu();
switch (mselect) {
case 1:
add_t();
break;
case 2:
edit_t();
break;
default: // If an error is encountered.
JOptionPane.showMessageDialog(null, "Oh dear! Error!");
break;
}
} while (mselect < 3);
}
但是,您应该使用 display_menu()
的返回值
public OnlineStore() // Switch-case program
{
do {
switch (display_menu()) {
case 1:
add_t();
break;
case 2:
edit_t();
break;
default: // If an error is encountered.
JOptionPane.showMessageDialog(null, "Oh dear! Error!");
break;
}
} while (mselect < 3);
}
要非常警惕静态
,它不是你的 friend ,并且可以很容易地将一个很好的运行程序变成垃圾。
相反,你可以做一些更像......的事情
public class OnlineStore {
String[] ColorType = {"blue", "green", "black"};
final int COLOURS = 3; // t choices
int[] Color = new int[COLOURS];
int sum;
public int display_menu() // Not the main program but the main menu.
{
String input;
input = JOptionPane.showInputDialog("Welcome!" + "\n\n1. Add tshirt order\n2. Edit tshirt order\n3. View current order\n4. Checkout" + "\n\nPlease enter your choice: ");
// return Integer.parseInt(input);
return Integer.parseInt(input);
}
public OnlineStore() // Switch-case program
{
boolean exit = false;
do {
switch (display_menu()) {
case 1:
add_t();
break;
case 2:
edit_t();
break;
case 4:
exit = true;
break;
default: // If an error is encountered.
JOptionPane.showMessageDialog(null, "Oh dear! Error!");
break;
}
} while (!exit);
}
public final int add_t() {
return 0;
}
public final int edit_t() {
return 0;
}
public static void main(String[] args) // Main program
{
new OnlineStore(); // Call out the program.
}
}
关于java - JOptionPane switch 语句循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32085213/