[英]Condensing basic code using || operator with a string
我是java编程的新手。 我无法找到有关使用||的任何信息 运算符与字符串。 我想知道是否有更有效的方法来执行此代码,特别是仍然易于阅读。 我尝试使用一个简单的计算器来熟悉IfThenElse语句。
import java.util.Scanner;
public class SimpleCalculator {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
double first;
double second;
String option;
while(true){
System.out.println("What function would you like to calculate?");
option=input.next();
if(option.equals("add") || option.equals("+")){
System.out.println("First number");
first=input.nextDouble();
System.out.println("Second number");
second=input.nextDouble();
double add=first+second;
System.out.println(add);
}
else if(option.equals("subtract") || option.equals("-")) {
System.out.println("First number");
first=input.nextDouble();
System.out.println("Second number");
second=input.nextDouble();
double subtract=first-second;
System.out.println(subtract);
}
else if(option.equals("multiply") ||option.equals("*")) {
System.out.println("First number");
first=input.nextDouble();
System.out.println("Second number");
second=input.nextDouble();
double multiply=first*second;
System.out.println(multiply);
}
else if(option.equals("divide") || option.equals("/")) {
System.out.println("First number");
first=input.nextDouble();
System.out.println("Second number");
second=input.nextDouble();
double divide=first/second;
System.out.println(divide);
}
else if(option.equals("end")){
System.exit(0);
}
}
}
}
在大多数情况下,我想知道如果要求,我已经测试过它们确实有效,但对我来说似乎有点笨拙。 但是,任何批评都会受到高度赞赏。
switch / case语句是一系列ifs的不错选择, 从Java 7开始,你可以使用带有字符串的switch语句。 注意两者之间的语法差异。 每个案例都以break
语句结尾,而不是用花括号分组。
switch (option) {
case "add":
case "+":
System.out.println("First number");
first=input.nextDouble();
System.out.println("Second number");
second=input.nextDouble();
double add=first+second;
System.out.println(add);
break;
case "subtract":
case "-":
System.out.println("First number");
first=input.nextDouble();
System.out.println("Second number");
second=input.nextDouble();
double subtract=first-second;
System.out.println(subtract);
break;
case "multiply":
case "*":
System.out.println("First number");
first=input.nextDouble();
System.out.println("Second number");
second=input.nextDouble();
double multiply=first*second;
System.out.println(multiply);
break;
case "divide":
case "/":
System.out.println("First number");
first=input.nextDouble();
System.out.println("Second number");
second=input.nextDouble();
double divide=first/second;
System.out.println(divide);
break;
case "end":
System.exit(0);
}
然后我会建议组合重复的提示代码。 如果您发现自己正在复制和粘贴代码,那么退后一步并找出如何避免重复通常是一个好主意。 重复的代码表明您应该进行一些重构。
if (option.equals("end")) {
System.exit(0);
}
System.out.println("First number");
first=input.nextDouble();
System.out.println("Second number");
second=input.nextDouble();
switch (option) {
case "add":
case "+":
double add=first+second;
System.out.println(add);
break;
case "subtract":
case "-":
double subtract=first-second;
System.out.println(subtract);
break;
case "multiply":
case "*":
double multiply=first*second;
System.out.println(multiply);
break;
case "divide":
case "/":
double divide=first/second;
System.out.println(divide);
break;
}
此外,您还可以通过对所有计算使用单个result
变量来消除重复的打印输出。
if (option.equals("end")) {
System.exit(0);
}
System.out.println("First number");
first=input.nextDouble();
System.out.println("Second number");
second=input.nextDouble();
double result;
switch (option) {
case "add": case "+": result = first + second; break;
case "subtract": case "-": result = first - second; break;
case "multiply": case "*": result = first * second; break;
case "divide": case "/": result = first / second; break;
}
System.out.println(result);
你使用的是||
对我来说似乎很好。 但是我有一些一般性的建议可以使代码整体更好。
首先,为什么不具备isAdd
, isSubtract
等功能? 例如:
private boolean isAdd(String input){
return input.equalsIgnoreCase("add") || input.equals("+");
}
其他运营商也是如此。 比你可以拥有如下代码:
if (isAdd(option)) ...
哪个更具可读性
if (input.equalsIgnoreCase("add") || input.equals("+")) ...
在一个更大的程序中,您可能需要多次检查这些类型的东西,然后有一个方法来做这个变得非常方便。 此外,这种方式如果您想要更改“添加”的定义(例如现在“a”也符合条件),您可以在一个地方更改代码并且整个程序符合要求。
其次,为什么不将这些if
语句的主体提取到其他函数中呢? 比你的代码读的那样:
if (isAdd(option))
performAddition();
else if (isSubtract(option))
performSubtraction();
// .. etc
// function definitions here
制作一个更易读的程序,而不是你现在拥有的程序。
第三,注意放置空间的位置。 option = input.next()
看起来比option=input.next()
更好。
这就是它。 祝好运 :)
John Kugelman和Aviv Cohn都提出了很好的建议。 我想补充一点,如果你在调用nextDouble()
没有输入有效数字,你的应用程序将抛出一个InputMismatchException
。 由于异常而不是您的程序终止,您可以提示用户输入有效的数字,之后他/她可以再试一次。
一种方法是将以下方法添加到SimpleCalculator
:
private static Double getValidNumber()
{
Double nr = null;
while( nr == null )
{
nr = getNextDouble();
if(nr == null) System.out.println("Please enter a valid number.");
}
return nr;
}
private static Double getNextDouble()
{
Scanner input=new Scanner(System.in);
Double output = null;
try{ output = input.nextDouble(); }catch(InputMismatchException e){}
return output;
}
然后在main方法中,只需用getValidNumber()
替换对input.nextDouble()
的调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.