Java int calculator:重新使用已计算的值进行进一步计算



我使用整数开发了一个简单的计算器(没有GUI),运行良好。计算器部署了七种类型的计算。目标和成就如下:

目标是:

  • 计算所有类型的计算(完成)
  • 部署继续计算选项,切换操作符,退出(完成)
  • 部署一个选项,继续计算结果(正在进行的工作)

我想知道如何使用计算结果用于另一个计算. 我尝试将特定结果的变量存储在相应的switch-case选项中,这是在MySimpleIntCalculatorHelper类中实现的(代码在下面的文档中)。不幸的是,这会抛出一个错误,并阻止计算器正常工作。你能告诉我什么建议吗?也许我监督着一些很明显的东西!

有关源代码的任何进一步信息,如果需要,我会给您留下到我的GitHub存储库的链接。https://https://github.com/DarkPalad1n/ExamPreCondition/tree/master/src

提前感谢!

亲切的问候,Passi

import java.util.Scanner;
/**
* This class is as an extension of the class MySimpleIntCalculatorAdvanced.
*
* It inherits methods from its parental class and deploys useful methods for
* running the calculator itself.
*
* @author DarkPalad1n
*
*/
public class MySimpleIntCalculatorHelper extends MySimpleIntCalculatorAdvanced {
static Scanner scanObject = new Scanner(System.in);
static MySimpleIntCalculatorHelper erg = new MySimpleIntCalculatorHelper();
static int operator = scanObject.nextInt();
static int numberOne, numberTwo, numberOneAd;
static String continueOption;
// ---------------------CONSTRUCTOR--------------------
public MySimpleIntCalculatorHelper() {
chooseOperator();
}
// --------------------METHODS--------------------
/**
* User enters the menu. User's able to choose between seven operations.
*/
public static void chooseOperator() {
System.out.println(
"Please enter your operator of choice: n1.Addition n2.Subtraction n3.Multiplication n4.Division n5.Remainder n6.Sum n7.Checksum n0.Exit");
}
/**
* Method is used to take the user's figure inputs.
*
* The condition checks first whether the operator is 0 or 7 before entering the
* cases where two figures are required.
*
* Case 0 ends the calculation.
*
* Case 7 only needs one input, which is why the method checks beforehand the
* chosen case to take the correct input.
*/
public static void chooseNumberOneAndTwo() {
if (operator == 0) {
System.out.println("Thank you for participating!");
System.exit(0);
} else if (operator == 7) {
System.out.println("Please enter your number:");
numberOneAd = scanObject.nextInt();
} else {
System.out.println("Please enter your first number:");
numberOne = scanObject.nextInt();
System.out.println("Please enter your second number:");
numberTwo = scanObject.nextInt();
}
}
/**
* Method is used to perform mathematical operations in conjunction with the
* chosen operator.
*/
public static void performCalculation() {
switch (operator) {
case 1:
erg.add(numberOne, numberTwo);
break;
case 2:
erg.sub(numberOne, numberTwo);
break;
case 3:
erg.mult(numberOne, numberTwo);
break;
case 4:
erg.div(numberOne, numberTwo);
break;
case 5:
erg.mod(numberOne, numberTwo);
break;
case 6:
erg.sum(numberOne, numberTwo);
break;
case 7:
erg.quer(numberOneAd);
break;
case 0:
System.out.println("Thank you for participating!");
System.exit(0);
default:
System.out.println("Unsupported operator. Please retry.");
}
}
/**
* This method asks the user whether to continue another calculation or not.
*
* scanObject.nextLine(); consumes the leftovers n from .nextInt();. After
* this workaround continueOption is ready for the user's input.
*/
public static void continueCalculationOption() {
System.out.println("Would you like to continue? n Enter Y to continue. n Enter N to exit. n Enter S to switch the operator.");
scanObject.nextLine();
continueOption = scanObject.nextLine();
}
/**
* Method is used to perform the users input from continueCalculationOption();.
*
* By entering "Y" the user is redirected to chooseNumberOneAndTwo(); to enter
* new figures for calculation with the same operator.
*
* By entering "N" the user is going to exit the calculator.
*
* By entering "S" the user is able to switch the operator. It is important to
* assign (zuweisen) operator to scanObject.nextInt(); for a proper performance
* of the new operator input.
*/
public static void performContinueCalculationOption() {
switch (continueOption) {
case "Y":
System.out.println("Continue.");
chooseNumberOneAndTwo();
break;
case "N":
System.out.println("Thank you for participating.");
System.exit(0);
break;
case "S":
System.out.println("Switch operator.");
chooseOperator();
operator = scanObject.nextInt();
break;
}
}
/**
* Method is used to perform at least a sequence of three instructions
* while continueOption is equal to "Y".
*/
public static void performCalculationSequence() {
do {
performCalculation();
continueCalculationOption();
performContinueCalculationOption();
} while (continueOption.equals("Y"));
}
/**
* This method uses a sequence of the helping methods created in this class
* to run all the functionalities the calculator offers. The method runs in
* the main method of the corresponding class.
*
* It consists of chooseNumberOneAndTwo(); and performCalculationSequence();.
* There is a condition if the user's input equals "S" the user is able to
* switch operators. After this particular method was used and the new
* operator was selected, new figures are required and the calculation will
* be performed. run(); will be executed continuously as long as a different
* operator is chosen or "N" is entered.
*
*/
public static void run() {
chooseNumberOneAndTwo();
performCalculationSequence();
if (continueOption.equals("S")) {
chooseNumberOneAndTwo();
performCalculationSequence();
run();
} else {
System.out.println();
}
}
}

您可以为您的类添加一个静态属性并将结果保存在其中。要选择这个数字,您可以修改chooseNumberOneAndTwo(),使其接受一个命令来选择最后的结果。(如"Ans")

检测,必须将scanObject.nextInt()改为scanObject.next()。要检查它是否是一个数字,您可以使用numberOne.matches("-?\d+")。解释:

  • - ?:第一个字符是'-';? 表示可选(对于负整数)
  • \d: d表示数字字符;''在代码
  • 中是\
  • +:有一个或多个前面的字符

完成的方法可以像这样(previousResult是保存最后结果的静态属性):

public static void chooseNumberOneAndTwo() {
String inp;
if (operator == 0) {
System.out.println("Thank you for participating!");
System.exit(0);
} else if (operator == 7) {
System.out.println("Please enter your number:");
inp = scanObject.next();
if (inp.matches("-?\d+")) {
numberOneAd = Integer.parseInt(inp);
}
else if (inp.matches("Ans")) {
numberOneAd = previousResult;
}
else {}//Invalid input
} else {
System.out.println("Please enter your first number:");
inp = scanObject.next();
if (inp.matches("-?\d+")) {
numberOne = Integer.parseInt(inp);
}
else if (inp.matches("Ans")) {
numberOne = previousResult;
}
else {}//Invalid input
System.out.println("Please enter your second number:");
inp = scanObject.next();
if (inp.matches("-?\d+")) {
numberTwo = Integer.parseInt(inp);
}
else if (inp.matches("Ans")) {
numberTwo = previousResult;
}
else {}//Invalid input
}
}

我还建议您为此创建一个新方法,因为该方法会重复执行相同的操作3次。

最新更新