一年赚10亿,二十年能赚多少钱。
money = 1000000000,years = 20
如果你用一下代码:
public class Shujuzhuanxing {
public static void main (String[] args){
int money = 1000000000;
int years = 20;
int total = money*years;
System.out.println(total);
}
}
输出结果为下图:
这是一个负数,为什么呢,因为int的范围大约+21亿到-21亿,200亿已经超过了int的值。所以溢出了,想要正常得到结果就需要提升int为long,办法很简单,就是在数值前面加(long)。
代码如下:
public class Shujuzhuanxing {
public static void main (String[] args){
int money = 1000000000;
int years = 20;
long total = (long)money*years;
System.out.println(total);
}
}
运行得到的结果如图:
转载于:https://blog.51cto.com/12869346/1921003