问题及代码:
/*
*Copyright (c)2015,西南大学计信院
*All rights reserved.
*文件名称:Helloworld.java
*作 者:高硕
*完成日期:2015年10月15日
*版 本 号:v1.0
*问题描述:通过年利率等来计算月支付额和支付总额。
*程序输入:年利率、时间、金额。
*程序输出:月支付额和总支付额。
*/
package practice_01;
import java.util.Scanner;
public class ComputeLoan {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
System.out.println("please input the yearly rate");
double yearlyrate =input.nextDouble();
double monthlyrate=yearlyrate/1200;
System.out.println("please input the number of years");
int number =input.nextInt();
System.out.println("please input the amount");
double amount =input.nextDouble();
double monthlypayment=amount* monthlyrate/1-1/Math.pow(1+monthlyrate,number*12);
double totalpayment=monthlypayment*number*12;
System.out.println("the monthly payment is " + (int)(monthlypayment*100) /100.0) ;
System.out.println("the total payment is " + (int)(totalpayment)*100 ) ;
}
}
运行结果:
知识点总结:
数据类型的强制转换(显式转换),Math.pow,优先级
心得体会:
感觉java的优先级和其他差不多。java对类型要求特别严格,不会全部进行自动转换。小类型变大类型可以拓宽,大类型变小类型如果不进行转换的话就会出错。
还有就是变量类型不同赋值时不转换就会出错。比如将int赋值给short或byte.
刚开始没有感觉有多麻烦,现在看来的确有点麻烦。。还有不太懂面向对象的意思。嗷嗷~~
最后记录一个知识点:java里输出语句里有一些“ ‘ 换行、回车、tab等不能用,可以用转义字符,比如双引号是 \"
单引号是 \' 腿哥是\b tab是\t 换行 \n 回车 \r 反斜杠 \\