我不知道我的标题是否解释清楚。我是java的新手(1周)。如果我使用某些单词或表达错误,请原谅我。的xD
我刚刚制作了自己的计算器。我没有其他来源的任何帮助,我做了很多。很高兴自己开始记住Java语法。 :d
这是我制作的计算器的代码:
// Calc with the switch statement.
import java.util.Scanner;
public class Calcswitch { 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是否有一种记录用户输入的方法,并以动态的方式存储它们以后容易回忆?
答案 0 :(得分: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;