Java 编写一个程序,接受两个数字和一个运算符(如 (+,-,*,/))作为命令行参数,并执行运算符指示的操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26083064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 01:49:08  来源:igfitidea点击:

Write a program that accepts two numbers and a operator like (+,-,*, /) as command line arguments and perform the operation indicated by operator

java

提问by Prince

Write a program that accepts two numbers and a operator like (+,-,*, /) as command line arguments and perform the appropriate operation indicated by operator.If the user enters any other character the appropriate message will be displayed. The output of the program should be displayed to the user.

编写一个程序,接受两个数字和一个运算符(如(+、-、*、/))作为命令行参数,并执行运算符指示的适当操作。如果用户输入任何其他字符,将显示适当的消息。程序的输出应该显示给用户。

My Code is this

我的代码是这个

public class Practical4
{

    public static void main(String[] args)
    {
        if(args.length==0)
        {
        System.out.println("No arguments are passed");
        }
        else
        {

        int a=Integer.parseInt(args[0]);
        String p=args[1];
        int b=Integer.parseInt(args[2]);

        switch(p)
        {
            case "+":
                System.out.println("Addition of "+a+" and "+b+" : "+(a+b));
                break;

            case "-":
                System.out.println("Subtraction of "+a+" and "+b+" : "+(a-b));
                break;

            case "*":
                System.out.println("Multiplication of "+a+" and "+b+" : "+(a*b));
                break;

            case "/":
                System.out.println("Division of "+a+" and "+b+" : "+(a/b));
                break;

            case "%":
                System.out.println("Modulo of "+a+" and "+b+" : "+(a%b));
                break;


            default:
                System.out.println("Please Enter '+', '-', '*', '/' & '%' operator only.");
        }
        }

    }

}

and i am getting this Error

我收到这个错误

java:17: incompatible types
found   : java.lang.String
required: int
        switch(p)
               ^
1 error

Please Give the Solution. Thanks

请给出解决方案。谢谢

采纳答案by Rustam

try this:because java 7onward stringis supported in switchcase.

试试这个:因为 java 7以后string在支持switch的情况下。

before java 7you should do like this:

java 7你应该这样做之前:

public class Practical4
{

    public static void main(String[] args)
    {
        if(args.length==0)
        {
        System.out.println("No arguments are passed");
        }
        else
        {

        int a=Integer.parseInt(args[0]);
        char p=args[1].charAt(0);
        int b=Integer.parseInt(args[2]);

        switch(p)
        {
            case '+':
                System.out.println("Addition of "+a+" and "+b+" : "+(a+b));
                break;

            case '-':
                System.out.println("Subtraction of "+a+" and "+b+" : "+(a-b));
                break;

            case '*':
                System.out.println("Multiplication of "+a+" and "+b+" : "+(a*b));
                break;

            case '/':
                System.out.println("Division of "+a+" and "+b+" : "+(a/b));
                break;

            case '%':
                System.out.println("Modulo of "+a+" and "+b+" : "+(a%b));
                break;


            default:
                System.out.println("Please Enter '+', '-', '*', '/' & '%' operator only.");
        }
        }

    }

}

回答by Kyborek

In old versions of java, you cannot iterate in switch over a string. That is allowed only since java 7: https://stackoverflow.com/a/12521398/1276062Either update your java version or convert the p to char

在旧版本的 java 中,您不能在切换字符串时进行迭代。仅自 java 7 以来才允许: https://stackoverflow.com/a/12521398/1276062 更新您的 java 版本或将 p 转换为 char