下面我写的这个代码在输出的时候数字由于小数的浮动很难对齐希望给予帮助..import javax.swing.JOptionPane;
public class Two { public static void main(String arg[]){
//prompt the user to enter the information
String loanString = JOptionPane.showInputDialog(null,
"Please enter loan amount \nFor example 10000.2:","Input",JOptionPane.QUESTION_MESSAGE);//get the loan amount
int loan = Integer.parseInt(loanString);// change the loan form string to int
String yearString = JOptionPane.showInputDialog(null,
"Please enter the number of years as an integer \nFor example 2:","Input",JOptionPane.QUESTION_MESSAGE);//get the year
int year = Integer.parseInt(yearString);// change the year form string to int
String rateString = JOptionPane.showInputDialog(null,
"Please enter the yearly rate \nFor example 3.4","Input",JOptionPane.QUESTION_MESSAGE);//get the yearly rate
double rate = Double.parseDouble(rateString);// change the rate form string to double
//calcuate monthlyrate,monthlypayment
double monthlyrate = (rate/1200);
double monthlypayment = (loan*monthlyrate / (1-(1/Math.pow((1+monthlyrate), year*12))));
double totalpayment = monthlypayment*year*12;
//declare variables
double principal = monthlypayment;
double interest =0;
double balance = loan;
//format to keep two digits after the decimal point
monthlypayment = (int)(monthlypayment*100) / 100.0;
totalpayment = Math.round(totalpayment*100) / 100.0;
String outputnumber= "";
String output="Loan Amount: "+loan+"\nNumber of Year: "+year+"\nAnnual interest rate: "
+(int)rate+"%"+"\nMonthly Payment: "+monthlypayment+"\nTotal Payment: "+totalpayment
+"\nPayment#"+" "+"interest"+" "+"Principal"+" "+"Balance";
/*calculate the interest,principal and balance
and put them into output*/
for (int i=1; i<=year;i++){
for(int j=1;j<=12;j++){
interest = balance * monthlyrate;
interest = (int)(interest*100) / 100.0;
principal = monthlypayment - interest;
balance = balance - principal;
principal = (int)(principal*100) / 100.0;
balance = (int)(balance*100) / 100.0;
outputnumber = " "+interest+" "+principal+" "+balance;
output+="\n"+i+"-"+j +outputnumber;
}
}
JOptionPane.showMessageDialog(null,
output,"output",JOptionPane.INFORMATION_MESSAGE);
}
}
public class Two { public static void main(String arg[]){
//prompt the user to enter the information
String loanString = JOptionPane.showInputDialog(null,
"Please enter loan amount \nFor example 10000.2:","Input",JOptionPane.QUESTION_MESSAGE);//get the loan amount
int loan = Integer.parseInt(loanString);// change the loan form string to int
String yearString = JOptionPane.showInputDialog(null,
"Please enter the number of years as an integer \nFor example 2:","Input",JOptionPane.QUESTION_MESSAGE);//get the year
int year = Integer.parseInt(yearString);// change the year form string to int
String rateString = JOptionPane.showInputDialog(null,
"Please enter the yearly rate \nFor example 3.4","Input",JOptionPane.QUESTION_MESSAGE);//get the yearly rate
double rate = Double.parseDouble(rateString);// change the rate form string to double
//calcuate monthlyrate,monthlypayment
double monthlyrate = (rate/1200);
double monthlypayment = (loan*monthlyrate / (1-(1/Math.pow((1+monthlyrate), year*12))));
double totalpayment = monthlypayment*year*12;
//declare variables
double principal = monthlypayment;
double interest =0;
double balance = loan;
//format to keep two digits after the decimal point
monthlypayment = (int)(monthlypayment*100) / 100.0;
totalpayment = Math.round(totalpayment*100) / 100.0;
String outputnumber= "";
String output="Loan Amount: "+loan+"\nNumber of Year: "+year+"\nAnnual interest rate: "
+(int)rate+"%"+"\nMonthly Payment: "+monthlypayment+"\nTotal Payment: "+totalpayment
+"\nPayment#"+" "+"interest"+" "+"Principal"+" "+"Balance";
/*calculate the interest,principal and balance
and put them into output*/
for (int i=1; i<=year;i++){
for(int j=1;j<=12;j++){
interest = balance * monthlyrate;
interest = (int)(interest*100) / 100.0;
principal = monthlypayment - interest;
balance = balance - principal;
principal = (int)(principal*100) / 100.0;
balance = (int)(balance*100) / 100.0;
outputnumber = " "+interest+" "+principal+" "+balance;
output+="\n"+i+"-"+j +outputnumber;
}
}
JOptionPane.showMessageDialog(null,
output,"output",JOptionPane.INFORMATION_MESSAGE);
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货