【问题标题】:Calculator that logs the calculations for print on request计算器记录计算打印请求
【发布时间】:2016-03-02 18:04:30
【问题描述】:

我不知道我的标题是否非常具有解释性。我是 Java 新手(1 周)。如果我使用了一些错误的词或表达方式,请原谅我。 xD

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

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

// Calc with the switch statement.

导入 java.util.Scanner;

公共类 Calcswitch { 公共静态 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");
}

}

现在我想实现一个简洁的小功能来记录答案。如果用户想查看自从他开始使用该程序以来的计算,他将能够通过编写“日志”或类似的东西来做到这一点。

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

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

【问题讨论】:

    标签: logging calculator


    【解决方案1】:

    一个大字符串怎么样,为每个日志添加一个新行

    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;
    

    【讨论】:

    • 天才!没有那样做。这就像我昨天做的柜台一样。双倍总计 = 总计 + 新闻总和
    • 谢谢,很高兴能帮上忙。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多