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

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

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

问题

我有一个静态方法Das它会接受两个类型为Car的对象并返回id的平均值我在访问id以找到平均值时遇到了困难任何帮助将不胜感激

这是我的代码

public class Car {

private int id;
private String name;

public Car(int id, String name)
{
this.id = id;
this.name = name;
}

public static int Das( Car C1 , Car C2) {
{
return (C1.id + C2.id)/2 ;
}

// getter and setter

Test.java
public class Test {

public static void main(String[] args) {

Car car1 = new Car(1,"A");
Car car2 = new Car(2,"V");
double A= Das(car1,car2);
System.out.println(A);
}}
英文:

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

public class Car {

private int id;
private String name;

public Car(int id, String name)
{
this.id = id;
this.name = name;
}

public static int Das( Car C1 , Car C2) {
{
return (C1.id + C2.id)/2 ;
}

// getter and setter

Test.java

public class Test {

public static void main(String[] args) {

Car car1 = new Car(1,"A");
Car car2 = new Car(2,"V");
double A= Das(car1,car2);
System.out.println(A);
}}


</details>


# 答案1
**得分**: 1

以下是您要的翻译部分:

对于返回 `int` 类型且会丢失小数部分的情况,可以按以下方式进行:
```java
public static int findAveCustomerId(Customer C1, Customer C2) {
    return (C1.id + C2.id) / 2;
}

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

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

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

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

For a double return where you won't lose fractions

public static double findAveCustomerId(Customer C1 , Customer C2) {
   return (C1.id + C2.id)/2.; // notice the decimal point.
}

</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:

确定