我想将用户输入与字符串进行比较,如果无效,那么程序应该显示菜单.我该怎么做



我正在制作一个程序,该程序将计算在英国机场和海外机场之间运行航班的可能盈利能力。

在我代码的一部分中,我想将用户输入与字符串进行比较,如果用户输入无效,那么程序应该将用户返回到菜单,用户应该能够输入"我该怎么做?"?

我将强调我为此编写的代码,但我不确定出了什么问题。

代码:

import java.io.IOException;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Scanner;
class Main {
// menu method
static void menu() {
System.out.println("------------------------------------------------------");
System.out.println("| Enter 1 to input airport details                   |");
System.out.println("| Enter 2 to input flight details                    |");
System.out.println("| Enter 3 to enter price plan and calculate profit   |");
System.out.println("| Enter 4 to clear data                              |");
System.out.println("| Enter 5 to quit                                    |");
System.out.println("------------------------------------------------------");
}
// text file
public static void main(String[] argv) throws Exception {
BufferedReader myFile = new BufferedReader(new FileReader("Airports.txt"));
ArrayList<String> listOfLines = new ArrayList<>();
String line = myFile.readLine();
while (line != null) {
listOfLines.add(line);
line = myFile.readLine();
}
myFile.close();

// main code
Scanner scanner = new Scanner(System.in);
System.out.println("n" + "Welcome");
menu();
int menuChoice = scanner.nextInt();
if (menuChoice == 5) {
System.out.println("n" + "You have selected quit");
System.out.println("Program ending.");
} else if (menuChoice == 1) {
System.out.println("n" + "You have selected input airport details");
System.out.println("n" + "Enter 3 letter airport code for UK airport");
String ukCode = scanner.next();

// need help here below
**if (ukCode != "LPL" || ukCode != "BOH") {
menu();
menuChoice = scanner.nextInt();** 

// above bold, I want program to keep displaying menu and making user enter data again if they have not inputted the correct 3 digit code such as "LPL" or "BOH". 


}
}
}

}

我的代码输出:如果用户错误输入3位代码:

欢迎------------------------------------------------------|输入1以输入机场详细信息||输入2以输入航班详细信息||输入3以输入价格计划并计算利润||输入4清除数据||输入5退出|------------------------------------------------------1.您已选择输入机场详细信息输入3个字母的英国机场代码rfrw------------------------------------------------------|输入1以输入机场详细信息||输入2以输入航班详细信息||输入3以输入价格计划并计算利润||输入4清除数据||输入5退出|------------------------------------------------------1.//在第二次输入1之后不做任何事情

为了在用户输入正确数据之前显示菜单,您应该使用循环,然后您需要读取字符串并使用您想要的字符串类型(如LPL或BOH(进行验证。在菜单之后,将相同的字符串传递给switch语句,并为每个菜单项编写单独的switch用例。使用单独的类为每个案例编写逻辑,只需从开关案例中调用方法。

Check the below code:
---------------------
static void menu() {
Scanner s = new Scanner(System.in);
String st = "";
while(st!=5){
System.out.println("Enter 1 to input airport details");
System.out.println("Enter 2 to input flight details");
System.out.println("Enter 3 to enter price plan and calculate profit");
System.out.println("Enter 4 to clear data");
System.out.println("Enter 5 to quit");

System.out.println("Enter your choice : ");
st = s.next();    //Validate for proper string you want
switch(st){
case 1: 
System.out.println("Input Airport Details : ");
//read data and apply business logic and store data -- do your stuff here
case 2:
System.out.println("Input flight details ");
//read data and apply logic for your operation

case 3:
System.out.println("enter price plan and calculate profit");
// here you need to read price plan and write code for calculating profit
case 4://code to clear data

case 5:
break;  // to exit
}
}
}

//此代码将根据执行的逻辑重新遍历菜单项。希望你觉得这有帮助。

相关内容

最新更新