package com.imooc.test;
public class EnergySystem {
private final double[] energyBoxs;
public EnergySystem(int n,double initialEnergy){
this.energyBoxs = new double[n];
for(int i=0;i<energyBoxs.length;i++){
energyBoxs[i] = initialEnergy;
}
}
public void Transfer(int from,int to,double amount){
if(energyBoxs[from]<amount){
return;
}
energyBoxs[from] = energyBoxs[from] - amount;
energyBoxs[to] = energyBoxs[to] + amount;
/*System.out.print(Thread.currentThread().getName());
System.out.printf("从%d转移%10.2f单位能量到%d", from, amount, to);
System.out.printf(" 能量总和:%10.2f%n", getTotalEnergy());*/
System.out.print(Thread.currentThread().getName());
System.out.print("从"+from+"转移"+amount+"单位能量到"+ to);
System.out.println(" 能量总和:"+ getTotalEnergy());
}
public double getTotalEnergy(){
double sum = 0;
for (double d : energyBoxs) {
sum = sum + d;
}
return sum;
}
public int getBoxAmount(){
return energyBoxs.length;
}
}为什么用printf打印的比print和println打印的更加规范(上面只是部分代码,问题发生在《深入浅出的多线程》的“线程交互汽车人的担忧”那一课)视频中用的是printf,而我用println之后发现结果有很大不同,问题不是出在数值的精确度上(先无视精确度),这两种打印方法打印出来的东西’结构‘不同
添加回答
举报
0/150
提交
取消