如何像在我的代码中那样将方法相互连接当用户按 2 时它必须去吃零食但它运行代码去吃饭 java
how to connect the methods to each other like in my code when the user press 2 it will have to go to snack but instead it runs the code to meals java
import java.util.Scanner;
public class MP3
{
//这是我需要将每个方法连接到每个方法的主要代码。
//所以如果用户按 2 它将去吃零食,但即使你现在按 2 它也只是继续代码并去吃饭。
//我怎样才能让用户可以选择he/she喜欢的菜单?
public static void main(String[] args)
{
int Options_Snacks;
int Options_Snacks2;
int Options_Snacks3;
int Options_Drinks;
int Options_Drinks2;
int Options_Drinks3;
int diff,prod ;
int yes = 1;
int no = 2;
int End;
do {
mainmenu();
mainmeals();
mainsnacks();
Scanner myInput = new Scanner(System.in);
System.out.println("Would you like to order more: press " + yes +" for yes and " + no +" for no");
End = myInput.nextInt();
} while(End == 1);
}
// 此代码 运行 很流畅,但其他菜单不会 运行 当用户按下数字时它 运行 是 while 循环中的所有代码。
public static void mainmenu()
{
int Orders;
int Meals = 1;
int Snacks = 2;
int Drinks = 3;
int EXIT = 4;
Scanner myInput = new Scanner(System.in);
System.out.println("=======[STRESSFOOD]=====");
System.out.println("---------[ Menu ]-------");
System.out.println("1----------Meals--------");
System.out.println("2---------Snacks--------");
System.out.println("3---------Drinks--------");
System.out.println("4--------[ EXIT ]-------");
Orders = myInput.nextInt();
}
[here's the code I didn't get all in the picture][1]
}
下面的实现呢:
public static void main(String[] args) {
openMenu();
}
在菜单函数中使用 while 循环:
private static void openMenu() {
Scanner scan = new Scanner(System.in);
do {
System.out.println("1) Meals");
System.out.println("2) Snacks");
System.out.println("3) Drinks");
System.out.println("4) Exit");
System.out.print("Your choice ? ");
switch (scan.nextInt()) {
case 1:
openMealsMenu();
break;
case 2:
openSnacksMenu();
break;
case 3:
openDrinksMenu();
break;
case 4:
scan.close();
return;
default:
System.out.println("This is not a valid choice");
}
} while (true);
}
请注意,您只能在应用程序中使用一台扫描仪。例如,如果您需要在 openMealsMenu() 中使用扫描仪,请将其作为方法的参数传递,或者只需在 MP3 class 中声明一个静态扫描仪,因为您的方法在同一个 class 中。
顺便说一句,按照惯例,变量应该以小写字母开头,并且不要使用 _
(即 Options_Drinks3
变为 optionsDrinks3
)。
import java.util.Scanner;
public class MP3
{
//这是我需要将每个方法连接到每个方法的主要代码。 //所以如果用户按 2 它将去吃零食,但即使你现在按 2 它也只是继续代码并去吃饭。 //我怎样才能让用户可以选择he/she喜欢的菜单?
public static void main(String[] args)
{
int Options_Snacks;
int Options_Snacks2;
int Options_Snacks3;
int Options_Drinks;
int Options_Drinks2;
int Options_Drinks3;
int diff,prod ;
int yes = 1;
int no = 2;
int End;
do {
mainmenu();
mainmeals();
mainsnacks();
Scanner myInput = new Scanner(System.in);
System.out.println("Would you like to order more: press " + yes +" for yes and " + no +" for no");
End = myInput.nextInt();
} while(End == 1);
}
// 此代码 运行 很流畅,但其他菜单不会 运行 当用户按下数字时它 运行 是 while 循环中的所有代码。
public static void mainmenu()
{
int Orders;
int Meals = 1;
int Snacks = 2;
int Drinks = 3;
int EXIT = 4;
Scanner myInput = new Scanner(System.in);
System.out.println("=======[STRESSFOOD]=====");
System.out.println("---------[ Menu ]-------");
System.out.println("1----------Meals--------");
System.out.println("2---------Snacks--------");
System.out.println("3---------Drinks--------");
System.out.println("4--------[ EXIT ]-------");
Orders = myInput.nextInt();
}
[here's the code I didn't get all in the picture][1]
}
下面的实现呢:
public static void main(String[] args) {
openMenu();
}
在菜单函数中使用 while 循环:
private static void openMenu() {
Scanner scan = new Scanner(System.in);
do {
System.out.println("1) Meals");
System.out.println("2) Snacks");
System.out.println("3) Drinks");
System.out.println("4) Exit");
System.out.print("Your choice ? ");
switch (scan.nextInt()) {
case 1:
openMealsMenu();
break;
case 2:
openSnacksMenu();
break;
case 3:
openDrinksMenu();
break;
case 4:
scan.close();
return;
default:
System.out.println("This is not a valid choice");
}
} while (true);
}
请注意,您只能在应用程序中使用一台扫描仪。例如,如果您需要在 openMealsMenu() 中使用扫描仪,请将其作为方法的参数传递,或者只需在 MP3 class 中声明一个静态扫描仪,因为您的方法在同一个 class 中。
顺便说一句,按照惯例,变量应该以小写字母开头,并且不要使用 _
(即 Options_Drinks3
变为 optionsDrinks3
)。