正在创建计算器,但无法退出



我需要编码方面的帮助。我再次练习我的java编程,今天我正在创建一个与真实计算器具有相同功能的计算器,但我再次遇到错误并且无法再次弄清楚。

好的,我希望我的计算器工作的方式是,而不是像这样从用户那里逐行输入:-

在代码输出中

Enter Number: 1
Enter Operator (+,-, /, *, ^ (Power)  or  s (Square): +
Enter Number: 2
Ans: 3

我希望它计算用户何时按回车键,如下所示:-

我想要的输出

enter number: 1+2*4 
Ans: 12

因此,他们可以在按回车计算之前添加任意数量的长数字。用户应该能够在计算循环中使用计算器时将数字重置为。

在代码的开头,它将要求用户输入以继续或退出计算器。然后,如果继续,它将运行计算。计算器将循环运行,直到用户按 E 退出计算器,如果退出,它将退出代码。

这是我有错误的地方。首先,我不知道如何从循环计算器内部的循环中中断,其次,当用户按 E 时,代码开头应该退出计算器,但它没有。第三个错误是当用户使用正方形计算时,我希望它直接显示答案,而不是问另一个数字。

我想简化计算部分public static void main(String[] args)的代码。是否可以将开关盒放在方法中并在主电源内调用它?或者您对如何简化计算部分有什么建议吗?

请帮助我:(

public class TestingCalculator {
public static void main(String[] args){
double answer = 0;
double numA, numB;
char operator;
char activateCalc; 
boolean calculator = false;
System.out.println("Welcome to the Calculator.");
System.out.print(" Continue (Press Y) n Exit (Press E) n: ");
Scanner ans = new Scanner(System.in);
String a = ans.next();
activateCalc = a.charAt(0);
while (activateCalc != 'E' || activateCalc != 'e') {
Scanner input = new Scanner(System.in);
System.out.print("Enter number: "); 
String n =input.next();
numA = Double.parseDouble(n); 
while (calculator = true) {
//User enter their operator.
System.out.print("Enter Operator (+,-, /, *, ^ (Power)  or  s (Square): ");
operator = input.next().charAt(0);
System.out.print("Enter number: ");  //User enter the continues number
numB = input.nextDouble();

switch (operator) {
case '=':
System.out.print(answer);   
break;
case '+':
answer = add(numA,numB);  
break;
case '-':
answer =subtract(numA,numB);
break;
case '*':
answer = multiply(numA,numB);
break;
case '/':
answer = divide(numA,numB);
break;
case '^':
answer = power(numA, numB);
break;
case 's':
case 'S':
answer = Math.pow(numA, 2);
break;
}
//The calculation answer of the user input
System.out.println("Answer: " + answer);
numA = answer;
// to exit calculator.
System.out.println("Press E to Exit the calculator: ");
if (activateCalc = 'E' || activateCalc = 'e') {
break;
}
}  
}
ans.close();
}
//Method for the operators.
static double add(double numA, double numB) {
double answer =  numA + numB;
return answer;
}
static double subtract(double numA, double numB) {
double answer = numA - numB;
return answer;        
}
static double multiply(double numA, double numB) {
double answer = numA * numB;
return answer;   
}
static double divide(double numA, double numB) {
double answer = numA / numB;
return answer;
}    
static double power(double numA, double numB) {
int answer = (int) Math.pow(numA, numB);
return answer;
}    
static double Square(double numA, double numB) {
int answer = (int) Math.pow(numA, 2);
return answer;
}    
}

我没有尝试确定应用程序的问题,而是决定专注于生成一个按照您想要的方式工作的程序(使用您指定的输入)。代码有点大,但我试图给它留下很好的注释,以便您了解我所做的事情。我知道有些事情可能仍然有点令人困惑,所以我将模拟运行程序以尝试更清楚地了解它是如何工作的。

法典

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class TestingCalculator 
{
//-------------------------------------------------------------------------
//      Methods
//-------------------------------------------------------------------------
/**
* Evaluates a mathematical expression.
* 
* @param       line Mathematical expression. This line cannot have blank
* spaces
* @return      Result of this mathematical expression
*/
public static String calc(String line)
{
while (!hasOnlyNumbers(line)) {
// Checks if line has parentheses
if (line.contains("(")) {
// Get index of the most nested parentheses
int parentheses_begin = line.lastIndexOf("(");
int parentheses_end = line.substring(parentheses_begin).indexOf(")");
String ans = calc(line.substring(parentheses_begin+1, parentheses_end));
// Replaces content of parentheses with the result obtained
if (line.length()-1 >= parentheses_end+1)
line = line.substring(0,parentheses_begin)+ans+line.substring(parentheses_end+1);
else
line = line.substring(0,parentheses_begin)+ans;
}
// Checks if line has potentiation operator
else if (line.contains("^")) {
int opIndex = line.indexOf("^");
String n1 = extractFirstNumber(line, opIndex);
String n2 = extractSecondNumber(line, opIndex);
double ans = power(Double.valueOf(n1), Double.valueOf(n2));
line = calc(parseLine(line, n1, n2, opIndex, ans));
}
// Checks if line has square operator
else if (line.contains("s")) {
int opIndex = line.indexOf("s");
String n1 = extractFirstNumber(line, opIndex);
double ans = square(Double.valueOf(n1));
line = calc(parseLine(line, n1, opIndex, ans));
}
// Checks if line has multiplication operator
else if (line.contains("*")) {
int opIndex = line.indexOf("*");
String n1 = extractFirstNumber(line, opIndex);
String n2 = extractSecondNumber(line, opIndex);
double ans = multiply(Double.valueOf(n1), Double.valueOf(n2));
line = calc(parseLine(line, n1, n2, opIndex, ans));
}
// Checks if line has division operator
else if (line.contains("/")) {
int opIndex = line.indexOf("/");
String n1 = extractFirstNumber(line, opIndex);
String n2 = extractSecondNumber(line, opIndex);
double ans = divide(Double.valueOf(n1), Double.valueOf(n2));
line = calc(parseLine(line, n1, n2, opIndex, ans));
}
// Checks if line has sum operator
else if (line.contains("+")) {
int opIndex = line.indexOf("+");
String n1 = extractFirstNumber(line, opIndex);
String n2 = extractSecondNumber(line, opIndex);
double ans = add(Double.valueOf(n1), Double.valueOf(n2));
line = calc(parseLine(line, n1, n2, opIndex, ans));
}
// Checks if line has subtraction operator
else if (line.contains("-")) {
int opIndex = line.indexOf("-");
String n1 = extractFirstNumber(line, opIndex);
String n2 = extractSecondNumber(line, opIndex);
double ans = subtract(Double.valueOf(n1), Double.valueOf(n2));
line = calc(parseLine(line, n1, n2, opIndex, ans));
}
}
// Returns line only when it has only numbers
return line;
}
/**
* Checks if a line contains only numbers.
* 
* @param       line Line to be analyzed
* @return      If a line contains only numbers
*/
private static boolean hasOnlyNumbers(String line)
{
return line.matches("^[0-9.]+$");
}
/**
* Given a mathematical expression, replaces a subexpression for a value.
* 
* @param       line Mathematical expression
* @param       n1 Number to the left of the subexpression operator
* @param       n2 Number to the right of the subexpression operator
* @param       opIndex Operator index of the subexpression
* @param       ans Value that will replace the subexpression
* @return      New mathematical expression with the subexpression replaced
* by the value
*/
private static String parseLine(String line, String n1, String n2, int opIndex, double ans)
{
int lenFirstNumber = n1.length();
int lenSecondNumber = n2.length();
if (line.length()-1 >= opIndex+lenSecondNumber+1)
return line.substring(0, opIndex-lenFirstNumber)+ans+line.substring(opIndex+lenSecondNumber+1);
return line.substring(0, opIndex-lenFirstNumber)+ans;
}
/**
* Given a mathematical expression, replaces a subexpression for a value.
* 
* @param       line Mathematical expression
* @param       n1 Number to the left of the subexpression operator
* @param       opIndex Operator index of the subexpression
* @param       ans Value that will replace the subexpression
* @return      New mathematical expression with the subexpression replaced
* by the value
*/
private static String parseLine(String line, String n1, int opIndex, double ans)
{
int lenFirstNumber = n1.length();
if (line.length()-1 >= opIndex+2)
return line.substring(0, opIndex-lenFirstNumber)+ans+line.substring(opIndex+2);
return line.substring(0, opIndex-lenFirstNumber)+ans;
}
/**
* Extracts the first number from an operation. <br />
* <h1>Example:<h1> <br />
*  <b>Line:</b>        1+2*3   <br />
*  <b>opIndex:</b>     3       <br />
*  <b>Return:</b>      2       <br />
* 
* @param       line Mathematical expression
* @param       opIndex Index of the operator to which the number to be 
* extracted belongs to
* @return      Number to the left of the operator
*/
private static String extractFirstNumber(String line, int opIndex)
{
StringBuilder num = new StringBuilder();
int i = opIndex-1;
while (i>=0 && (Character.isDigit(line.charAt(i)) || line.charAt(i) == '.')) {
num.append(line.charAt(i));
i--;
}
// Reverses the result, since the number is taken from the end to the 
// beginning
num = num.reverse();
return num.toString();
}
/**
* Extracts the second number from a math operation. <br />
* <h1>Example:<h1> <br />
*  <b>Line:</b>        1+2*3   <br />
*  <b>opIndex:</b>     3       <br />
*  <b>Return:</b>      3       <br />
* 
* @param       line Mathematical expression
* @param       opIndex Index of the operator to which the number to be 
* extracted belongs to
* @return      Number to the right of the operator
*/
private static String extractSecondNumber(String line, int opIndex)
{
StringBuilder num = new StringBuilder();
int i = opIndex+1;
while (i<line.length() && (Character.isDigit(line.charAt(i)) || line.charAt(i) == '.')) {
num.append(line.charAt(i));
i++;
}
return num.toString();
}
// Method for the operators.
private static double add(double numA, double numB) 
{
double answer = numA + numB;
return answer;
}
private static double subtract(double numA, double numB) 
{
double answer = numA - numB;
return answer;
}
private static double multiply(double numA, double numB) 
{
double answer = numA * numB;
return answer;
}
private static double divide(double numA, double numB) 
{
double answer = numA / numB;
return answer;
}
private static double power(double numA, double numB) 
{
int answer = (int) Math.pow(numA, numB);
return answer;
}
private static double square(double num) 
{
int answer = (int) Math.pow(num, 2);
return answer;
}
//-------------------------------------------------------------------------
//      Main
//-------------------------------------------------------------------------
public static void main(String[] args) throws IOException 
{
char option;
String inputLine = "";
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Welcome to the Calculator.");
System.out.print(" Continue (Press Y) n Exit (Press E) n: ");
option = input.readLine().charAt(0);
while (option != 'E' && option != 'e') {
// Gets user input
System.out.print("Enter mathematical expression: ");
inputLine += input.readLine();
// Processes input
inputLine = inputLine.replaceAll(" ", "");
inputLine = inputLine.replaceAll("S", "s");
// Evaluates input
System.out.println("Evaluating...");
String ans = TestingCalculator.calc(inputLine);
// Displays answer
System.out.println("Ans: "+ans);
// Checks if the user wants to continue running the program
System.out.print("Press E to Exit the calculator: ");
inputLine = input.readLine();
if (inputLine.length() > 0)
option = inputLine.charAt(0);
}
input.close();
}
}

输出

Enter mathematical expression: (1+2*4)/3
Evaluating...
Ans: 3.0

产出2

Enter mathematical expression: 1+2*9/3
Evaluating...
Ans: 7.0

案头检查

输入:(1+2*4)/3

计算( (1+2*4)/3 ) not hasOnlyNumbers( (1+2*4)/3) ) ?真 ( (1+2*4)/3) )包含 '(' ? true 整数 parentheses_begin = 0 int parentheses_end = 6 字符串 ans = calc( 1+2*4 ) 计算( 1+2*4 ) 没有只有数字( 1+2*4 ) ?真 ( 1+2*4 ) 包含 '(' ? 假 ( 1+2*4 ) 包含 '^' ?假 ( 1+2*4 ) 包含 's' ?假 ( 1+2*4 ) 包含 '*' ?真 int opIndex = 3 整数 n1 = 2 整数 n2 = 4 字符串 ans = n1 * n2 = 2 * 4 = 8 线 = 计算( 1+8 ) 计算( 1+8 ) 没有只有数字( 1+8 ) ?真 ( 1+8 ) 包含 '(' ? 假 ( 1+8 ) 包含 '^' ?假 ( 1+8 ) 包含 's' ?假 ( 1+8 ) 包含 '*' ?假 ( 1+8 ) 包含 '/' ?假 ( 1+8 ) 包含 '+' ?真 int opIndex = 1 整数 n1 = 1 整数 n2 = 8 字符串 ans = n1 + n2 = 1 + 8 = 9 线 = 计算( 9 ) 计算( 9 ) 没有只有数字( 9 ) ?假 返回 9 行 = 9 没有只有数字( 9 ) ?假 返回 9 行 = 9 没有只有数字( 9 ) ?假 返回 9 ans = 9 (9-1>= 6+1) ?真 线 = 9/3 没有只有数字( 9/3 ) ?真 ( 9/3 ) 包含 '(' ? 假 ( 9/3 ) 包含 '^' ?假 ( 9/3 ) 包含 's' ?假 ( 9/3 ) 包含 '*' ?假 ( 9/3 ) 包含 '/' ?真 int opIndex = 1 字符串 n1 = 9 字符串 n2 = 3 双精度 ANS = 9/3 = 3 线 = 计算( 3 ) 计算( 3 ) 没有只有数字( 3 ) ?假 返回 3 行 = 3 没有只有数字( 3 ) ?假 返回 3

一些观察

  • 不会检查用户提供的输入是否有效
  • 保持 calc 方法中验证的运算顺序很重要,以保持运算符之间的优先级(必须首先进行幂/除法,然后是乘法/除法,最后是加法/减法运算)
  • 我在使用 Scanner 类时遇到了问题,所以我使用 BufferedReader 来读取输入
  • 平方运算必须按如下方式完成:s 或S

希望这有帮助。如果你有什么不明白的地方,告诉我我可以向你解释。

尝试下面的代码,一个建议也尝试处理负面情况。

public static void main(String[] args) {
double answer = 0;
double numA, numB;
char operator;
char activateCalc;
boolean calculator = false;
System.out.println("Welcome to the Calculator.");
System.out.print(" Continue (Press Y) n Exit (Press E) n: ");
Scanner ans = new Scanner(System.in);
Scanner input = new Scanner(System.in);
activateCalc = input.next().charAt(0);
while (true) {
if (activateCalc != 'E' && activateCalc != 'e') {
System.out.print("Enter number: ");
String n = input.next();
numA = Double.parseDouble(n);
// User enter their operator.
System.out.print("Enter Operator (+,-, /, *, ^ (Power)  or  s (Square): ");
operator = input.next().charAt(0);
System.out.print("Enter number: "); // User enter the continues number
numB = input.nextDouble();
switch (operator) {
case '=':
System.out.print(answer);
break;
case '+':
answer = add(numA, numB);
break;
case '-':
answer = subtract(numA, numB);
break;
case '*':
answer = multiply(numA, numB);
break;
case '/':
answer = divide(numA, numB);
break;
case '^':
answer = power(numA, numB);
break;
case 'S':
case 's':
answer = Math.pow(numA, 2);
break;
default:
answer = 0;
}
// The calculation answer of the user input
System.out.println("Answer: " + answer);
numA = answer;
// to exit calculator.
System.out.println("Press E to Exit the calculator or Y to continue : ");
activateCalc = input.next().charAt(0);
if(activateCalc != 'E' && activateCalc != 'e')continue;
}
System.out.println("Thank you for using the calculator. By :) ");
ans.close();
break;
}
}
// Method for the operators.
static double add(double numA, double numB) {
double answer = numA + numB;
return answer;
}
static double subtract(double numA, double numB) {
double answer = numA - numB;
return answer;
}
static double multiply(double numA, double numB) {
double answer = numA * numB;
return answer;
}
static double divide(double numA, double numB) {
double answer = numA / numB;
return answer;
}
static double power(double numA, double numB) {
int answer = (int) Math.pow(numA, numB);
return answer;
}
static double Square(double numA, double numB) {
int answer = (int) Math.pow(numA, 2);
return answer;
}

这个问题需要调试细节,所以我们来了:

  1. 由于错误,您的代码似乎无法编译:
if (activateCalc = 'E' || activateCalc = 'e') {
break;
}

您必须使用比较运算符而不是分配===运算符。

  1. 类似的问题存在于您的内部循环while (calculator = true)- 并且有一个警告说此值永远不会被使用 - 但这并没有太大影响。

  2. 您无法退出循环,因为您从不检查输入是否退出,它应该是:

System.out.println("Press E to Exit the calculator: ");
activateCalc = input.next().charAt(0);
  1. 但即使你更新了activateCalc,你无论如何都会因为这种情况的错误而进入无限循环while (activateCalc != 'E' || activateCalc != 'e')- 即使用户按"e"或"E",这种情况总是正确的。

相关内容

  • 没有找到相关文章

最新更新