跪求!Java编写一个方法,求三个整数平均值,平均值类型要求为双精度浮点型,在main方法中调用这个方法。

2024-11-28 15:45:50
推荐回答(3个)
回答1:

public class Test{
public static void main(String[] args) {

Scanner sc =new Scanner(System.in);
System.out.println("请输入第一个整数:");
int a = sc.nextInt();
System.out.println("请输入第二个整数:");
int b = sc.nextInt();
System.out.println("请输入第三个整数:");
int c = sc.nextInt();

Test t = new Test();
double avg = average( a,b, c);
System.out.println("三个数的平均数为:"+avg);

}

public double average(int a,int b,int c){
double avg = (double)((a+b+c)/3.0);
return avg;
}
}

回答2:

public static double avg(int a,int b,int c){
return (a+b+c)/3.0;
}

public static void main(String[] args){
int a=1,b=2,c=3;
System.out.println(avg(a,b,c));
}

回答3:

public static double average(int a,int b,int c){
return (a + b + c )/3.0;

}