如何在 Java 中添加 运行 次数的摘要报告?

How to add a summary report of how many times something has run in Java?

我正在尝试获取用户在退出程序后在计算器中做题的次数的总报告。我希望它看起来像这样:

计算器报告
加法题: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() {
  }
 }

每次调用计算器函数时递增的变量非常好。 我会像这样填写 printReport() 方法

public static void printReport() {
    System.out.println("Calculator Report");
    System.out.println("Addition problems: " + addCount);
    System.out.println("Substraction problems: " + subCount);
    System.out.println("Multiplication problems: " + mulCount);
    System.out.println("Division problems: " + divCount);
    System.out.println("Total Problems: " + (addCount + subCount + mulCount + divCount));
}

并将退出方式改为

public static void exit() {
    Calculator2.printReport();
    System.exit(0);
}

一定要在exit()方法中调用printReport()方法。并在 System.exit(0) 之前执行;线。否则不执行。

如果需要,可以复制此代码。我为你实现了方法:

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() {
        Calculator2.printReport();
        System.exit(0);
    }


    public static void printReport() {
        System.out.println("Calculator Report");
        System.out.println("Addition problems: " + addCount);
        System.out.println("Substraction problems: " + subCount);
        System.out.println("Multiplication problems: " + mulCount);
        System.out.println("Division problems: " + divCount);
        System.out.println("Total Problems: " + (addCount + subCount + mulCount + divCount));
    }
}