如何访问ID的值以求平均值?

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

How can I access value of ID to find the average?

问题

  1. 我有一个静态方法Das它会接受两个类型为Car的对象并返回id的平均值我在访问id以找到平均值时遇到了困难任何帮助将不胜感激
  2. 这是我的代码
  3. public class Car {
  4. private int id;
  5. private String name;
  6. public Car(int id, String name)
  7. {
  8. this.id = id;
  9. this.name = name;
  10. }
  11. public static int Das( Car C1 , Car C2) {
  12. {
  13. return (C1.id + C2.id)/2 ;
  14. }
  15. // getter and setter
  16. Test.java
  17. public class Test {
  18. public static void main(String[] args) {
  19. Car car1 = new Car(1,"A");
  20. Car car2 = new Car(2,"V");
  21. double A= Das(car1,car2);
  22. System.out.println(A);
  23. }}
英文:

I have static method Das which will take 2 objects of type Car and return average value of id. I have difficulty accessing id to find the average. Any help will be appreciated

Here is my code

  1. public class Car {
  2. private int id;
  3. private String name;
  4. public Car(int id, String name)
  5. {
  6. this.id = id;
  7. this.name = name;
  8. }
  9. public static int Das( Car C1 , Car C2) {
  10. {
  11. return (C1.id + C2.id)/2 ;
  12. }
  13. // getter and setter

Test.java

  1. public class Test {
  2. public static void main(String[] args) {
  3. Car car1 = new Car(1,"A");
  4. Car car2 = new Car(2,"V");
  5. double A= Das(car1,car2);
  6. System.out.println(A);
  7. }}
  8. </details>
  9. # 答案1
  10. **得分**: 1
  11. 以下是您要的翻译部分:
  12. 对于返回 `int` 类型且会丢失小数部分的情况,可以按以下方式进行:
  13. ```java
  14. public static int findAveCustomerId(Customer C1, Customer C2) {
  15. return (C1.id + C2.id) / 2;
  16. }

对于返回 double 类型且不会丢失小数部分的情况:

  1. public static double findAveCustomerId(Customer C1, Customer C2) {
  2. return (C1.id + C2.id) / 2.0; // 注意小数点
  3. }
英文:

For an int return where you will lose fractions, do it as follows.

  1. public static int findAveCustomerId(Customer C1 , Customer C2) {
  2. return (C1.id + C2.id)/2;
  3. }

For a double return where you won't lose fractions

  1. public static double findAveCustomerId(Customer C1 , Customer C2) {
  2. return (C1.id + C2.id)/2.; // notice the decimal point.
  3. }
  4. </details>

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

发表评论

匿名网友

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

确定