How can I do one-operand math with my Java calculator?
我已经尝试在Java中创建一个简单的计算器一段时间了,我已经成功地使程序使用两个操作数方程(+,-,*,/和^)。然而,我想知道我如何能够做一个操作数数学问题——绝对值(使用符号"")、平方根(使用符号"v")、四舍五入到最接近的整数(使用符号"~")、sin(s)、cos(c)和tangent(t)。
我尝试过绝对值操作数,可以在以下内容中看到:
1 2 3 4 |
还有:
1 2 3 4 5 |
仅当您输入以下值时,此代码才起作用:
任何帮助解决这个问题和帮助找出其他单操作数操作的帮助都将非常感谢!
事先谢谢!
我的程序的源代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | 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; } } |
我对您现有的代码做了一些修改,以便它能在所有情况下都适用,并允许将来扩展函数。你可以通过评论来理解这些变化。此外,如果用户只为函数提供一个输入,并且只有一个参数足够,那么代码将能够运行。我没有改变你的任何职能。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | 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; } } } |
输出:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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 |
使用正则表达式检查第一个数字是否实际为数字。然后做你想做的。此外,您可以使用常规异常处理错误的用户输入。所以如果你输入"3+3",你就不会有
1 2 3 4 5 6 7 8 9 10 | 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; } |