借入額c、年利r、年数nを入れると毎年の返済額と返済総額を答えるプログラムを作っています。
以下は私が作成したプログラムです。
import java.util.Scanner;
public class Repayment{
public static void main(String[] args) {
System.out.printf("\n毎年の返済額 %d\n",repay(1000000,0.05,10));
System.out.printf("\n返済総額 %d\n",repayB(1000000,0.05,10));
}
static int amountB(int c, double r, int n) { //元金cの元利合計
return(int)amount(c,r,n);
}
static int amountC(int x, double r, int n) { //積立預金の元利合計
double c=0;
for(int i=1; i<=n; i++) c += amount(x, r, i);
return (int)c;
}
static double amount(int c, double r, int n) { //元利合計・理論値
return c * Math.pow((1+r),n);
}
static int repay(int c, double r, int n) {//毎年の返済額 int x = c/n; int total = amountB(c, r, n); x = x/10*10; while(amountC(x,r,n-1)+x<total)x+=10; return (int)x;
}
static int repayB(int c,double r, double n){//返済総額
double y=0.0;
y +=c;
for(int i=0;i<n;i++){
y=y*(1+r);
y +=c;
}
return (double)y;
}
}
ちなみにコンパイルすると
Repayment.java:37: エラー: 不適合な型: 精度が失われる可能性があるdoubleからintへの変換
return (double)y;
というエラーがでました。
あと、最後の返済総額のメソッド自信ないのでみてもらえると助かりますよろしくお願いします。
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。