私は初心者としてJavaを練習しています。ここで質問です。毎月 100 ドルを年利 5% の普通預金口座に預けるとします。したがって、月利は 0.00417 です。最初の月の後、アカウントの値は 100 * (1 + 0.00417) = 100.417 になり、2 番目の月は (100 + firstMonthValue) * 1.00417 になり、その後は毎月同様に続きます。だからここに私のコードがあります:
import javax.swing.JOptionPane;
public class vinalcialApplication {
public static void main(String args[]){
String monthlySaving = JOptionPane.showInputDialog("Enter the monthly savings");
double monthsaving = Double.parseDouble(monthlySaving);
//define monthly rate
double monthlyrate = 1.00417;
double totalrate = monthlyrate + 0.00417;
double firstMonthValue = monthsaving * (totalrate);
double secondMonthValue = (firstMonthValue + 100)*(monthlyrate);
double thridMonthValue = (secondMonthValue + 100) * (monthlyrate);
.........
System.out.print("After the sixth month, the account value is " sixthMonthValue);
}
}
つまり、コードは機能しますが、書くにはコードが多すぎます..ループまたはifステートメントを使用してこれを行うことができると確信していますが、まだそれを行う方法を考え出していません..助けてもらえますか? ありがとうございました。