【发布时间】:2021-03-23 23:14:00
【问题描述】:
我正在尝试获取用户在退出程序后在计算器中完成问题的次数的总报告。我希望它看起来像这样:
计算器报告
加法题:3
减法问题:0
乘法问题:2
划分问题:1
总问题:6
下面是我的代码。代码的计算器部分有效,我想我已经设置了正确的计数变量,但在用户退出时无法创建报告。
import java.util.Scanner;
import java.math.*;
public class Calculator2
{
private static final Scanner askScanner = new Scanner(System.in);
public static int answer;
public static int firstNumber;
public static int secondNumber; //makes variables for the whole class
//Used for the Report at the end.
public static int addCount = 0;
public static int subCount = 0;
public static int mulCount = 0;
public static int divCount = 0;
public static void main(String[] args) {
calculator();
printReport();
}
public static void calculator() {
while (true) {
System.out.println("Basic calculator");
System.out.println("Pick one:");
System.out.println("(A)ddition");
System.out.println("(S)ubtraction");
System.out.println("(M)ultiplication");
System.out.println("(D)ivision");
System.out.println("\n(E)xit");
String line = askScanner.nextLine().toUpperCase(); //Allows for any input to be a capitol letter.
char pick = line.charAt(0);
//uses the input of the user and directs it to the correct opertation
if(pick == 'A') {
addition();
}
else if(pick == 'S') {
subtraction();
}
else if(pick == 'M') {
multiplication();
}
else if(pick == 'D') {
division();
}
else if(pick == 'E') {
exit();
}
else {
System.out.println("You need to choose A, S, M, D, or E");
}
} // end while
}
//asks the user for the 2 numbers
private static void getNumbers() {
System.out.print("Enter you first number: ");
firstNumber = askScanner.nextInt();
System.out.print("Enter your second number: ");
secondNumber = askScanner.nextInt();
askScanner.nextLine();
}
//the different operations based off what the user wanted to do plus the operation itself
public static void subtraction() {
System.out.println("Subtraction");
getNumbers();
answer = firstNumber - secondNumber;
System.out.println("This is the difference of the two numbers: " + answer);
subCount++;
}
public static void addition() {
System.out.println("Addition");
getNumbers();
answer = firstNumber + secondNumber;
System.out.println("This is the sum of the two numbers: " + answer);
addCount++;
}
public static void multiplication() {
System.out.println("Multiplication");
getNumbers();
answer = firstNumber * secondNumber;
System.out.println("This is the product of the two numbers " + answer);
mulCount++;
}
public static void division() {
System.out.println("Division");
getNumbers();
try{
answer = firstNumber / secondNumber;
System.out.println("This is the quotient of the two numbers: " + answer);
}
catch (ArithmeticException e) {
System.out.println("Cannot divide by zero!! Please enter another number to divide by." );
}
divCount++;
}
public static void exit() {
System.exit(0);
}
public static void printReport() {
}
}
【问题讨论】:
标签: java calculator