英文:
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("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);
答案1
得分: 0
以下是翻译好的内容:
所以我认为我基本上理解了您想要的内容。我无法使平均数前面没有零,但击球率和长打率都保留了三位小数。我修复的主要部分是将打数(bats)从整数更改为双精度值,并将模数函数更改为除法。以下是代码的外观。
    //每个击中次数的代码未显示
    
    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 = "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);
Let me know if you have any questions. Good luck.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论