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

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 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);

所以我想我基本上已经得到了你想要得到的东西。我无法让平均数前面没有零,但击球率和重击率都保留了小数点后 3 位。基本上我修复的是将 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);

如果您有任何问题,请告诉我。祝你好运。