# 问题在于小数点的位置。我有一个公式，需要平均值位于小数点右边。

go评论57阅读模式

Issue with decimal placement. I have a forumla and need the average to be on the right of the decimal place

# 问题

``````batavg = (single + Double + triple + hr) % bats;
slug = ((single) + (Double*2) + (triple*3) + (hr*4)) % bats;

System.out.println("选手姓名：" + player);
System.out.printf("当前选手击球率为：%.3f\n", batavg);
System.out.printf("当前选手长打率为：%.3f\n", slug);
``````

``````  batavg = (single + Double + triple + hr) % bats;
slug = ((single) + (Double*2) + (triple*3) + (hr*4)) % bats;

System.out.println(&quot;Player name: &quot; + player);
System.out.printf(&quot;The currnet players batting average is: %.3f\n&quot;,  batavg);
System.out.printf(&quot;The current players Slugging Average is : %.3f\n&quot;, slug);
``````

# 答案1

``````    //每个击中次数的代码未显示

double bats = 35.0;   //这是一个基准值
String player = "PLAYER";
double batavg;
double slug;
batavg = (single + Double + triple + hr) / bats;  //使用除号而不是%号
//(模数在这种情况下不起作用)
slug = ((single) + (Double*2) + (triple*3) + (hr*4)) / bats;

System.out.println("球员姓名：" + player);

System.out.printf("当前球员的击球率为：%.3f\n",  batavg);
//现在会有小数点和3位数字
System.out.printf("当前球员的长打率为：%.3f\n", slug);

``````

So I think I have basically what you are trying to get. I can't get the average to not have a zero in front, but both the Batting Average and Slugging Percentage come out to have 3 decimal places. Basically what I fixed was making the At-bats (bats) into a double value instead of an integer, and changing your modulus function into division. Here's what the code looks like.

``````//code for each int of hits are not shown

double bats = 35.0;   //this is used as a baseline
String player = &quot;PLAYER&quot;;
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(&quot;Player name: &quot; + player);

System.out.printf(&quot;The current players batting average is: %.3f\n&quot;,  batavg);
//now there will be a decimal and 3 numbers after
System.out.printf(&quot;The current players Slugging Percentage is : %.3f\n&quot;, slug);
``````

Let me know if you have any questions. Good luck.

• 本文由 发表于 2020年10月2日 09:01:23
• 转载请务必保留本文链接：https://go.coder-hub.com/64164995.html
• decimal
• java
• operation

go 82

go 66

go 76

go 60