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

huangapple go评论79阅读模式
英文:

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

问题

  1. batavg = (single + Double + triple + hr) % bats;
  2. slug = ((single) + (Double*2) + (triple*3) + (hr*4)) % bats;
  3. System.out.println("选手姓名:" + player);
  4. System.out.printf("当前选手击球率为:%.3f\n", batavg);
  5. System.out.printf("当前选手长打率为:%.3f\n", slug);
英文:
  1. batavg = (single + Double + triple + hr) % bats;
  2. slug = ((single) + (Double*2) + (triple*3) + (hr*4)) % bats;
  3. System.out.println("Player name: " + player);
  4. System.out.printf("The currnet players batting average is: %.3f\n", batavg);
  5. System.out.printf("The current players Slugging Average is : %.3f\n", slug);

答案1

得分: 0

以下是翻译好的内容:

所以我认为我基本上理解了您想要的内容。我无法使平均数前面没有零,但击球率和长打率都保留了三位小数。我修复的主要部分是将打数(bats)从整数更改为双精度值,并将模数函数更改为除法。以下是代码的外观。

  1. //每个击中次数的代码未显示
  2. double bats = 35.0; //这是一个基准值
  3. String player = "PLAYER";
  4. double batavg;
  5. double slug;
  6. batavg = (single + Double + triple + hr) / bats; //使用除号而不是%号
  7. //(模数在这种情况下不起作用)
  8. slug = ((single) + (Double*2) + (triple*3) + (hr*4)) / bats;
  9. System.out.println("球员姓名:" + player);
  10. System.out.printf("当前球员的击球率为:%.3f\n", batavg);
  11. //现在会有小数点和3位数字
  12. System.out.printf("当前球员的长打率为:%.3f\n", slug);
  13. 如果您有任何问题请随时向我询问祝您好运
英文:

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.

  1. //code for each int of hits are not shown
  2. double bats = 35.0; //this is used as a baseline
  3. String player = "PLAYER";
  4. double batavg;
  5. double slug;
  6. batavg = (single + Double + triple + hr) / bats; //division sign used instead of %
  7. //(modulus will not work for this)
  8. slug = ((single) + (Double*2) + (triple*3) + (hr*4)) / bats;
  9. System.out.println("Player name: " + player);
  10. System.out.printf("The current players batting average is: %.3f\n", batavg);
  11. //now there will be a decimal and 3 numbers after
  12. System.out.printf("The current players Slugging Percentage is : %.3f\n", slug);

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

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

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定