Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Java计算器计算一个操作数?_Java_Math_If Statement_Operators_Calculator - Fatal编程技术网

如何使用Java计算器计算一个操作数?

如何使用Java计算器计算一个操作数?,java,math,if-statement,operators,calculator,Java,Math,If Statement,Operators,Calculator,我尝试用Java创建一个简单的计算器已有一段时间了,我已经成功地使程序能够使用两个操作数方程(+、-、*、/、和^)。但是,我想知道如何才能解决一个操作数的数学问题—绝对值(使用符号“|”)、平方根(使用符号“v”)、舍入到最接近的整数(使用符号“~”)、sin(s)、cos(c)和切线(t) 我尝试了绝对值操作数,可以在以下内容中看到: if (operator == '|') { answer = Math.abs(numA); } // In the main cl

我尝试用Java创建一个简单的计算器已有一段时间了,我已经成功地使程序能够使用两个操作数方程(+、-、*、/、和^)。但是,我想知道如何才能解决一个操作数的数学问题—绝对值(使用符号“|”)、平方根(使用符号“v”)、舍入到最接近的整数(使用符号“~”)、sin(s)、cos(c)和切线(t)

我尝试了绝对值操作数,可以在以下内容中看到:

if (operator == '|') {
            answer = Math.abs(numA);
}
// In the main class
以及:

此代码仅在您输入值时有效,例如:
-3 |-3
(注意:我注意到它只是执行绝对值操作的第一个数字。第二个数字可以是您想要的任何数字(如果您输入
-3 |-4
,您的答案仍然是
3
)只要它确实是一个数字

如果您能为解决此问题提供任何帮助,并帮助您找出其他单操作数操作,我们将不胜感激

提前谢谢

我的程序的源代码如下:

package calculator;
import java.util.Scanner;

public class Calculator {

public static void main(String[] args) {

    System.out.println("Hello, welcome to my calculator");
    System.out.println("Enter in some stuff you want to me to calculate");

    Scanner scan = new Scanner(System.in);
    System.out.println("If you need help please type \"help\"");
    System.out.println("If at anytime you want to leave, type \"quit\"");
    System.out.println("Hit enter to continue.");

    String s1 = scan.nextLine();

    if (s1.equals("help")){
        System.out.println(" ");

        System.out.println("Double operand commands:");
        System.out.println("Addition: '+' (Ex: 'a + b' )");
        System.out.println("Subtraction: '-' (Ex: 'a - b' )");
        System.out.println("Multiplication: '*' (Ex: 'a * b' ) ");
        System.out.println("Division: '/' (Ex: 'a / b' )");
        System.out.println("Exponents: '^' (Ex: 'a ^ b' )");

        System.out.println(" ");
    }

    Scanner input = new Scanner(System.in);

    Maths maths = new Maths();

    double answer = 0;
    double numA, numB;
    char operator;
    boolean quit = false;


    while (true) {

    System.out.print("Please enter your equation: ");

    String s=input.next();

    if(s.equals("quit")){
        System.out.println("Thank you for using my program!");
        System.exit(0);
    }

    numA = Double.parseDouble(s);
    operator = input.next().charAt(0);
    numB = input.nextDouble();        

    if (operator == '+') {
        answer = maths.add(numA, numB);
    }

    if (operator == '-') {
        answer = maths.subtract(numA, numB);
    }

    if (operator == '*') {
        answer = maths.multiply(numA, numB);
    }

    if (operator == '/') {
        answer = maths.divide(numA, numB);
    }

    if (operator == '^') {
        answer = maths.power(numA, numB);
    }

    if (operator == '|') {
        answer = Math.abs(numA);
    }

        System.out.println(answer);        



        }

    }

}

class Maths {

    double add(double a, double b) {
        double answer = a+b;
        return answer;          
    }

    double subtract(double a, double b) {
        double answer = a-b;
        return answer;          
    }

    double multiply(double a, double b) {
        double answer = a*b;
        return answer;          
    }

    double divide(double a, double b) {
        double answer = a/b;
        return answer;          
    }

    double power(double a, double b){
        double answer =a;

        for (int x=2; x<=b; x++){
            answer *= a;
        }

        return answer;
    }

    double absolute(double a) {
        double answer = Math.abs(a);
        return answer;
    }


}
package计算器;
导入java.util.Scanner;
公共类计算器{
公共静态void main(字符串[]args){
System.out.println(“您好,欢迎使用我的计算器”);
System.out.println(“输入一些您希望我计算的内容”);
扫描仪扫描=新扫描仪(System.in);
System.out.println(“如果需要帮助,请键入“帮助”);
System.out.println(“如果您想随时离开,请键入\“退出\”);
System.out.println(“按回车键继续”);
字符串s1=scan.nextLine();
如果(s1.等于(“帮助”)){
System.out.println(“”);
System.out.println(“双操作数命令:”);
System.out.println(“加法:'+'(例如:'a+b')”;
System.out.println(“减法:'-'(例如:'a-b')”;
System.out.println(“乘法:'*'(例如:'a*b')”;
System.out.println(“部门:'/'(例如:“a/b”);
System.out.println(“指数:'^'(例如:'a^b')”;
System.out.println(“”);
}
扫描仪输入=新扫描仪(System.in);
数学=新数学;
双答案=0;
双核,麻木;
字符算子;
布尔退出=假;
while(true){
System.out.print(“请输入公式:”);
字符串s=input.next();
如果(s.equals(“退出”)){
System.out.println(“感谢您使用我的程序!”);
系统出口(0);
}
numA=Double.parseDouble(s);
运算符=输入.next().charAt(0);
numB=input.nextDouble();
if(运算符=='+'){
答案=数学。加(numA,numB);
}
if(运算符=='-'){
答案=数学。减法(numA,numB);
}
如果(运算符=='*'){
答案=数学。乘法(numA,numB);
}
if(运算符=='/')){
答案=数学。除法(numA,numB);
}
if(运算符=='^'){
答案=数学。力量(numA,麻木);
}
if(运算符==“|”){
答案=Math.abs(numA);
}
System.out.println(应答);
}
}
}
课堂数学{
双加(双a,双b){
双重回答=a+b;
返回答案;
}
双减法(双a,双b){
双重回答=a-b;
返回答案;
}
双乘(双a,双b){
双重回答=a*b;
返回答案;
}
双除法(双a,双b){
双重回答=a/b;
返回答案;
}
双电源(双a、双b){
双重回答=a;

对于(int x=2;x使用正则表达式检查第一个数字是否实际上是一个数字。然后执行相应的操作。此外,您可以使用正则异常处理错误的用户输入。因此,如果输入“3+3”,您将不会有
java.lang.NumberFormatException
s


我对您现有的代码进行了一些修改,使其能够适应所有情况,并允许将来扩展函数。您可以通过注释了解更改。此外,如果用户只为函数提供一个输入,并且只有一个参数就足够,则代码将能够运行。我没有更改您的任何函数

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class Calculator {

    public static void main(String[] args) {

        System.out.println("Hello, welcome to my calculator");
        System.out.println("Enter in some stuff you want to me to calculate");

        Scanner scan = new Scanner(System.in);
        System.out.println("If you need help please type \"help\"");
        System.out.println("If at anytime you want to leave, type \"quit\"");
        System.out.println("Hit enter to continue.");

        String s1 = scan.nextLine();

        if (s1.equals("help")) {
            System.out.println(" ");

            System.out.println("Double operand commands:");
            System.out.println("Addition: '+' (Ex: 'a + b' )");
            System.out.println("Subtraction: '-' (Ex: 'a - b' )");
            System.out.println("Multiplication: '*' (Ex: 'a * b' ) ");
            System.out.println("Division: '/' (Ex: 'a / b' )");
            System.out.println("Exponents: '^' (Ex: 'a ^ b' )");

            System.out.println(" ");
        } else if (s1.equals("quit")) {
            System.out.println("Thank you for using my program!");
            System.exit(0);
        }

        Scanner input = new Scanner(System.in);


        Maths maths = new Maths();

        double answer = 0;
        double numA=0.0, numB=0.0;
        char operator;
        boolean quit = false;

        while (true) {

            System.out.print("Please enter your equation: ");

            //First scan the function as a string
            String s = input.next();

            if (s.equals("quit")) {
                System.out.println("Thank you for using my program!");
                System.exit(0);
            }

          //We will use regex to find the operator, so we will omit all alphabetic letter or numeric number or decimal
            String operator1 = s.replaceAll("[a-zA-Z0-9.]",""); 
          //For functions like -4|, the operator1 will be -| after replacing through regex, we will only take the second digit as operator to prevent error
            if(operator1.length()==1) 
            operator = operator1.charAt(0);
            else
                operator = operator1.charAt(1); 
            String[] num11 = (s.split("[^0-9,.]"));
        //String array num11 may contain null string after splitting using regex, we will remove those null string and store only variable values in an arraylist
           ArrayList<String> arraylist = new ArrayList<String>();

            for (int i = 0; i < num11.length; i++)
            {
                if (!num11[i].equals(""))
                {
                    arraylist.add(num11[i]);
                }
            }



            if(arraylist.size()==1){
            numA = Double.parseDouble(arraylist.get(0));    
            numB=numA;}
            else if(arraylist.size()==2){
            numA = Double.parseDouble(arraylist.get(0));    
            numB = Double.parseDouble(arraylist.get(1));

            }




            if (operator == '+') {
                answer = maths.add(numA, numB);
            }

            if (operator == '-') {
                answer = maths.subtract(numA, numB);
            }

            if (operator == '*') {
                answer = maths.multiply(numA, numB);
            }

            if (operator == '/') {
                answer = maths.divide(numA, numB);
            }

            if (operator == '^') {
                answer = maths.power(numA, numB);
            }

            if (operator == '|') {
                answer = Math.abs(numA);
            }

            System.out.println(answer);

        }

    }

    public static class Maths {

        public void Maths(){};

        double add(double a, double b) {
            double answer = a + b;
            return answer;
        }

        double subtract(double a, double b) {
            double answer = a - b;
            return answer;
        }

        double multiply(double a, double b) {
            double answer = a * b;
            return answer;
        }

        double divide(double a, double b) {
            double answer = a / b;
            return answer;
        }

        double power(double a, double b) {
            double answer = a;

            for (int x = 2; x <= b; x++) {
                answer *= a;
            }

            return answer;
        }

        double absolute(double a) {
            double answer = Math.abs(a);
            return answer;
        }

    }

}

除非操作员需要,否则不要执行
numB=input.nextDouble()
。@谢谢!我该怎么做呢?不是很优雅,但如果需要,您可以将该行移到
中的每一行(operator=
需要它的块。这是可行的…但就像你说的,不是很优雅,我有点想把操作数放在所说的数字之前,而不是后面。在这种情况下,你最好把整行作为一个字符串来读,并根据需要对其进行解析。(看第一个词,看看它是否是一个可以去那里的操作员等)谢谢塔瑞克…出于好奇,有没有办法做同样的事情…除了不使用Rebecca你说的Rebecca是什么意思?Regex?如果你指的是Regex,那么是的,有办法。但是Regex是最好的,而且很容易学习。
if (s.matches("^[-+]?[0-9]*\\.?[0-9]+$")) { //Check whether first number is actually a number
    numA = Double.parseDouble(s);
    operator = input.next().charAt(0);
    numB = input.nextDouble();

} else {
    operator = s.charAt(0);
    numA = input.nextDouble();
    numB = 0;
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class Calculator {

    public static void main(String[] args) {

        System.out.println("Hello, welcome to my calculator");
        System.out.println("Enter in some stuff you want to me to calculate");

        Scanner scan = new Scanner(System.in);
        System.out.println("If you need help please type \"help\"");
        System.out.println("If at anytime you want to leave, type \"quit\"");
        System.out.println("Hit enter to continue.");

        String s1 = scan.nextLine();

        if (s1.equals("help")) {
            System.out.println(" ");

            System.out.println("Double operand commands:");
            System.out.println("Addition: '+' (Ex: 'a + b' )");
            System.out.println("Subtraction: '-' (Ex: 'a - b' )");
            System.out.println("Multiplication: '*' (Ex: 'a * b' ) ");
            System.out.println("Division: '/' (Ex: 'a / b' )");
            System.out.println("Exponents: '^' (Ex: 'a ^ b' )");

            System.out.println(" ");
        } else if (s1.equals("quit")) {
            System.out.println("Thank you for using my program!");
            System.exit(0);
        }

        Scanner input = new Scanner(System.in);


        Maths maths = new Maths();

        double answer = 0;
        double numA=0.0, numB=0.0;
        char operator;
        boolean quit = false;

        while (true) {

            System.out.print("Please enter your equation: ");

            //First scan the function as a string
            String s = input.next();

            if (s.equals("quit")) {
                System.out.println("Thank you for using my program!");
                System.exit(0);
            }

          //We will use regex to find the operator, so we will omit all alphabetic letter or numeric number or decimal
            String operator1 = s.replaceAll("[a-zA-Z0-9.]",""); 
          //For functions like -4|, the operator1 will be -| after replacing through regex, we will only take the second digit as operator to prevent error
            if(operator1.length()==1) 
            operator = operator1.charAt(0);
            else
                operator = operator1.charAt(1); 
            String[] num11 = (s.split("[^0-9,.]"));
        //String array num11 may contain null string after splitting using regex, we will remove those null string and store only variable values in an arraylist
           ArrayList<String> arraylist = new ArrayList<String>();

            for (int i = 0; i < num11.length; i++)
            {
                if (!num11[i].equals(""))
                {
                    arraylist.add(num11[i]);
                }
            }



            if(arraylist.size()==1){
            numA = Double.parseDouble(arraylist.get(0));    
            numB=numA;}
            else if(arraylist.size()==2){
            numA = Double.parseDouble(arraylist.get(0));    
            numB = Double.parseDouble(arraylist.get(1));

            }




            if (operator == '+') {
                answer = maths.add(numA, numB);
            }

            if (operator == '-') {
                answer = maths.subtract(numA, numB);
            }

            if (operator == '*') {
                answer = maths.multiply(numA, numB);
            }

            if (operator == '/') {
                answer = maths.divide(numA, numB);
            }

            if (operator == '^') {
                answer = maths.power(numA, numB);
            }

            if (operator == '|') {
                answer = Math.abs(numA);
            }

            System.out.println(answer);

        }

    }

    public static class Maths {

        public void Maths(){};

        double add(double a, double b) {
            double answer = a + b;
            return answer;
        }

        double subtract(double a, double b) {
            double answer = a - b;
            return answer;
        }

        double multiply(double a, double b) {
            double answer = a * b;
            return answer;
        }

        double divide(double a, double b) {
            double answer = a / b;
            return answer;
        }

        double power(double a, double b) {
            double answer = a;

            for (int x = 2; x <= b; x++) {
                answer *= a;
            }

            return answer;
        }

        double absolute(double a) {
            double answer = Math.abs(a);
            return answer;
        }

    }

}
Please enter your equation: +4+4
8.0
Please enter your equation: 4+4
8.0
Please enter your equation: 4+3
7.0
Please enter your equation: 4-3
1.0
Please enter your equation: 4/3
1.3333333333333333
Please enter your equation: -4|
4.0
Please enter your equation: 4|
4.0
Please enter your equation: 3^2
9.0