英文:
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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论