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是否有一種記錄用戶輸入的方法,並以動態方式存儲,以後很容易調用?
天才!沒有關於這方面的事情。這就像我昨天在櫃檯上做的那樣。 Double total = total + newsum – Hhhfpe
謝謝,我很高興能幫上忙。 –