简体   繁体   English

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

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

I am trying to get a total report for how many times a user has done a problem in a calculator after they exit the program.我正在尝试获取用户在退出程序后在计算器中完成问题的次数的总报告。 I would like it to look like this:我希望它看起来像这样:

Calculator Report计算器报告
Addition problems: 3加法题:3
Subtraction problems: 0减法问题:0
Multiplication problems: 2乘法题:2
Division problems: 1分工问题:1
Total problems: 6总问题:6

Below is my code.下面是我的代码。 The calculator part of the code works and I think I have set up the correct counting variables but cannot get it to create a report when the user exits.代码的计算器部分有效,我想我已经设置了正确的计数变量,但在用户退出时无法让它创建报告。

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() {
  }
 }

The variables that you increment every time you call a calculator function are really nice.每次调用计算器 function 时递增的变量非常好。 What I would is fill in the printReport() method like so我会像这样填写 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));
}

And change the exit method to并将退出方法更改为

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

Be sure to call the printReport() method in the exit() method.请务必在 exit() 方法中调用 printReport() 方法。 And do it before the System.exit(0);并在 System.exit(0); 之前执行。 line.线。 Otherwise it won't execute.否则不会执行。

If you want, you can copy this code.如果需要,您可以复制此代码。 I implemented the methods for you:我为你实现了这些方法:

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));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM