记录计算结果以按要求打印的计算器

Calculator that logs the calculations for print on request

不知道我的标题是不是很说明问题。我是 java 的新手(1 周内)。如果我用错了某些词或表达方式,请原谅我。 xD

我刚刚制作了自己的计算器。我几乎没有任何其他来源的帮助就做到了这一点。很高兴开始自己记住 Java 语法。 :D

这是我制作的计算器的代码:

// Calc with the switch statement.

导入java.util.Scanner;

public class 计算开关 { public static void main(String[] args) {

    // Different variables

    Scanner input = new Scanner(System.in);

    double fnum = 0;
    double snum = 0;
    double answer = 0;
    int whatOperation;

    String finalquestion = "";
    boolean calculateAgain = true; // Goes inside the while loop


    while (calculateAgain) {
    // Ask what type of calculation you want to perform.
    System.out.println("\n");
    System.out.println("Press 1 for +");
    System.out.println("Press 2 for -");
    System.out.println("Press 3 for *");
    System.out.println("Press 4 for /");

    whatOperation = input.nextInt();


    switch (whatOperation) {

    case 1:
        // Addition
        System.out.print("Enter first number:  ");
        fnum = input.nextInt();
        System.out.print("Enter second number:  ");
        snum = input.nextInt();

        System.out.print("The result is:  ");
        System.out.print(fnum + snum);
        break;

    case 2:
        // Subtraction
        System.out.print("Enter first number:  ");
        fnum = input.nextInt();
        System.out.print("Enter second number:  ");
        snum = input.nextInt();

        System.out.print("The result is:  ");
        System.out.print(fnum - snum);
        break;

    case 3:
        // Multiplication
        System.out.print("Enter first number:  ");
        fnum = input.nextInt();
        System.out.print("Enter second number:  ");
        snum = input.nextInt();

        System.out.print("The result is:  ");
        System.out.print(fnum * snum);
        break;

    case 4:
        // Division
        System.out.print("Enter first number:  ");
        fnum = input.nextInt();
        System.out.print("Enter second number:  ");
        snum = input.nextInt();

        System.out.print("The result is:  ");
        System.out.print(fnum / snum);  
        break;

        default: 
            System.out.print("You have entered an invalid number. ");
            break;

        }

    // Asks if you want to use the Calculator again
    System.out.print("\n\nDo you want to use the calculator again? Yes / No:   ");
    finalquestion = input.next();

    // If no, the string calculateAgain goes false, and exit the while loop.
    if (finalquestion.equalsIgnoreCase("no")) {


        calculateAgain = false;


    }


    }

    // After the while loop. 
    System.out.println("Thank you for using this calculator");
}

}

现在我想实现一个简洁的小功能来记录答案。如果用户想查看自开始使用该程序以来的计算结果,他可以通过编写 "log" 或类似的内容来实现。

问题是。我不确定如何记录答案。一种方法是制作很多字符串并将它们留空。但是,我想这不会很动态。如果我声明了 10 个用于记录的字符串,但用户进行了 15 次计算,会发生什么情况?它不会很好用。

java 是否有记录用户输入的方法,并以动态方式存储它们以便日后轻松调用?

一个大字符串呢,每条日志加一行

String LOG = "LOG INFO";

// WHEN YOU WANT TO ADD A LOGENTRY TO YOUR LOG
String NewLogEntry = "REPLACE_WITH_YOUR_LOG_INFO";
LOG = LOG + /n + NewLogEntry;