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

1 回答 1

0

所以我想我基本上有你想要得到的东西。我不能让平均值前面没有零,但是击球平均值和重击百分比都有 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);

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

于 2020-10-02T01:39:12.817 回答