@MadProgrammer,切换案例功能在一个选择后停止。我似乎无法回到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.
}
}
答案 0 :(得分:1)
看起来你并没有给mselect一个值。您可能想要做的是
fseek()/ftell()
答案 1 :(得分:1)
首先...
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.
}
}
答案 2 :(得分:0)
您正在将输入分配给局部变量。其范围仅适用于该方法。 local Variable scope
mselect = Integer.parseInt(input);
return mselect;
并指定
mselect = display_menu();
或将mselect设为静态并删除局部变量。