如何简化此方法Java
我目前正在从事该任务,而我的代码在处理它的过程中变得太长了。 我得到了多个输入,如果语句变得太长了。有什么办法可以通过使用其他语句(例如while)简化它?
private void menu() {
String rentorpurchase = myObj.nextLine();
if (whichmovie2.equals("Batman") && rentorpurchase.equals("rent")){
System.out.println ("How long would you like to rent (days) ?");
int rentperiod = myObj.nextInt();
M1.getMovieRP(rentperiod);
}else if (whichmovie2.equals("Batman") && rentorpurchase.equals("purchase")){
System.out.println ("Cash or card?");
String Payoption = myObj.nextLine();
M1.getMoviePP(Payoption);
}else if (whichmovie2.equals("Ironman") && rentorpurchase.equals("rent")){
System.out.println ("How long would you like to rent (days) ?");
int rentperiod = myObj.nextInt();
M2.getMovieRP(rentperiod);
}else{
System.out.println ("Cash or card?");
String Payoption = myObj.nextLine();
M2.getMoviePP(Payoption);
}else{
System.out.println ("which TvShow you would like to rent?");
String whichmovie2 = myObj.nextLine();
System.out.println ("would you like to rent or purchase?");
String rentorpurchase = myObj.nextLine();
}
if (whichmovie2.equals("Friends") && rentorpurchase.equals("rent")){
System.out.println ("How long would you like to rent (days) ?");
int rentperiod = myObj.nextInt();
T1.getRP(rentperiod);
}else if (whichmovie2.equals("Friends") && rentorpurchase.equals("purchase")){
System.out.println ("Cash or card?");
String Payoption = myObj.nextLine();
T1.getPP(Payoption);
}else if(whichmovie2.equals("Sherlock") && rentorpurchase.equals("rent")){
System.out.println ("How long would you like to rent (days) ?");
int rentperiod = myObj.nextInt();
T2.getRP(rentperiod);
}else{
System.out.println ("Cash or card?");
String Payoption = myObj.nextLine();
T2.getPP(Payoption);
}
}
I am currently working on the assignment and my code became too long while working on it.
I got multiple inputs and the if statement became too long. is there any way I can simplify it by using another statement such as while?
private void menu() {
String rentorpurchase = myObj.nextLine();
if (whichmovie2.equals("Batman") && rentorpurchase.equals("rent")){
System.out.println ("How long would you like to rent (days) ?");
int rentperiod = myObj.nextInt();
M1.getMovieRP(rentperiod);
}else if (whichmovie2.equals("Batman") && rentorpurchase.equals("purchase")){
System.out.println ("Cash or card?");
String Payoption = myObj.nextLine();
M1.getMoviePP(Payoption);
}else if (whichmovie2.equals("Ironman") && rentorpurchase.equals("rent")){
System.out.println ("How long would you like to rent (days) ?");
int rentperiod = myObj.nextInt();
M2.getMovieRP(rentperiod);
}else{
System.out.println ("Cash or card?");
String Payoption = myObj.nextLine();
M2.getMoviePP(Payoption);
}else{
System.out.println ("which TvShow you would like to rent?");
String whichmovie2 = myObj.nextLine();
System.out.println ("would you like to rent or purchase?");
String rentorpurchase = myObj.nextLine();
}
if (whichmovie2.equals("Friends") && rentorpurchase.equals("rent")){
System.out.println ("How long would you like to rent (days) ?");
int rentperiod = myObj.nextInt();
T1.getRP(rentperiod);
}else if (whichmovie2.equals("Friends") && rentorpurchase.equals("purchase")){
System.out.println ("Cash or card?");
String Payoption = myObj.nextLine();
T1.getPP(Payoption);
}else if(whichmovie2.equals("Sherlock") && rentorpurchase.equals("rent")){
System.out.println ("How long would you like to rent (days) ?");
int rentperiod = myObj.nextInt();
T2.getRP(rentperiod);
}else{
System.out.println ("Cash or card?");
String Payoption = myObj.nextLine();
T2.getPP(Payoption);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用开关和案例语句从上面的选项之一中选择。
You could use a switch and case statements to select from one of the options above.
从您的if/then/then/the Switch的链中更改将使代码更具可读性,但仍然是一个笨拙的块。特别是如果您想添加更多增强功能,您会遇到问题。
因此,我建议您创建一个方法,以打印一个问题和循环以获取输入,直到输入有效为止。您需要定义有效的内容 - 只有任何值,一个数字,或...
然后您可以使用这些问题方法来使用这些问题方法 - 在这里,您可能会重复使用已经编写的内容。这不仅使您的代码更短,更容易理解,您现在可以更轻松地维护它。
Changing from your chain of if/then/else to switch will make the code a bit more readable but it still remains a clunky block. Especially if you want to add more enhancements you will run into problems.
So I suggest for every question you create a method that prints the question and loops for getting input until the input is valid. You need to define what is valid - just any value, or a number, or ...
Then you can have your clunky block make use of these question methods - and here you can potentially reuse stuff you already wrote. This not only makes your code shorter and easier to understand, you can now more easily maintain it.