加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 使用||压缩基本代码带字符串的运算符

发布时间:2020-12-15 04:44:09 所属栏目:Java 来源:网络整理
导读:我是 java编程的新手.我无法找到有关使用||的任何信息运算符与字符串.我想知道是否有更有效的方法来执行此代码,特别是仍然易于阅读.我尝试使用一个简单的计算器来熟悉IfThenElse语句. import java.util.Scanner; public class SimpleCalculator { public sta
我是 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的不错选择,而 as of 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;
}

此外,您还可以通过对所有计算使用单个结果变量来消除重复的打印输出.

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);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读