如何解决小数点位置问题我有一个论坛,需要平均值在小数点右边
batavg = (single + Double + triple + hr) % bats;
slug = ((single) + (Double*2) + (triple*3) + (hr*4)) % bats;
System.out.println("Player name: " + player);
System.out.printf("The currnet players batting average is: %.3f\n",batavg);
System.out.printf("The current players Slugging Average is : %.3f\n",slug);
解决方法
所以我想我基本上已经掌握了您想要得到的。我不能使平均值前面没有零,但是击球平均值和慢跑百分比都小数点后三位。基本上,我解决的问题是将At-bats(bats)变成双精度值而不是整数,然后将模数函数更改为除法。这是代码的样子。
//code for each int of hits are not shown
double bats = 35.0; //this is used as a baseline
String player = "PLAYER";
double batavg;
double slug;
batavg = (single + Double + triple + hr) / bats; //division sign used instead of %
//(modulus will not work for this)
slug = ((single) + (Double*2) + (triple*3) + (hr*4)) / bats;
System.out.println("Player name: " + player);
System.out.printf("The current players batting average is: %.3f\n",batavg);
//now there will be a decimal and 3 numbers after
System.out.printf("The current players Slugging Percentage is : %.3f\n",slug);
如果您有任何疑问,请告诉我。祝你好运。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。