2016-03-02 66 views
0

我不知道我的标题是否非常具有说明性。我是新来的Java(1周)。如果我用一些词或表达方式错误,请原谅我。 xD根据请求记录计算打印的计算器

我刚刚制作了我自己的计算器。没有其他来源的帮助,我做得非常好。很高兴开始记住我自己的Java语法。 :d

这是我提出的计算器的代码:

// Calc with the switch statement. 

进口java.util.Scanner中;

公共类Calcswitch { 公共静态无效的主要(字串[] 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是否有一种记录用户输入的方法,并以动态方式存储,以后很容易调用?

回答

0

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

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; 
+1

天才!没有关于这方面的事情。这就像我昨天在柜台上做的那样。 Double total = total + newsum – Hhhfpe

+0

谢谢,我很高兴能帮上忙。 –