JOptionPane 切换语句循环
JOptionPane switch statement loop
@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.
}
}
您似乎没有为 mselect 赋值。您可能想要做的是
int sum;
static int 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);
mselect = Integer.parseInt(input);
return 0;
}
您正在将输入分配给局部变量。它的范围只与方法有关。 local Variable scope
mselect = Integer.parseInt(input);
return mselect;
并分配
mselect = display_menu();
或将 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;
}
您没有返回 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
,它没有任何价值可以签入您的 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);
}
非常警惕 static
,它不是你的朋友,很容易将一个不错的 运行 程序变成垃圾。
相反,您可以做更像...
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.
}
}
@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.
}
}
您似乎没有为 mselect 赋值。您可能想要做的是
int sum;
static int 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);
mselect = Integer.parseInt(input);
return 0;
}
您正在将输入分配给局部变量。它的范围只与方法有关。 local Variable scope
mselect = Integer.parseInt(input);
return mselect;
并分配
mselect = display_menu();
或将 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;
}
您没有返回 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
,它没有任何价值可以签入您的 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);
}
非常警惕 static
,它不是你的朋友,很容易将一个不错的 运行 程序变成垃圾。
相反,您可以做更像...
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.
}
}