Java程序查找三个数字的平均值

编写一个Java程序,使用加号(+)和除号(/)算术运算符来查找三个数字的平均值。首先,它接受三个double类型的数字,然后使用加法运算符将它们相加,并使用除法运算符来查找平均值。

package RemainingSimplePrograms;

import java.util.Scanner;

public class AvgOfThree1 {
	private static Scanner sc;
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		System.out.print("Please Enter the first Number = ");
		double num1 = sc.nextDouble();
		
		System.out.print("Please Enter the Second Number = ");
		double num2 = sc.nextDouble();
		
		System.out.print("Please Enter the Third Number = ");
		double num3 = sc.nextDouble();
		
		double average = (num1 + num2 + num3)/ 3;
		System.out.println("\nThe Average of Three Numbers = " + average);
	}
}
Java Program to find the Average of Three Numbers

在这个示例中,我们创建了一个calAvg函数,该函数查找并返回三个数字的平均值。

package RemainingSimplePrograms;

import java.util.Scanner;

public class AvgOfThree2 {
	private static Scanner sc;
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		System.out.print("Please Enter the first = ");
		double num1 = sc.nextDouble();
		
		System.out.print("Please Enter the Second = ");
		double num2 = sc.nextDouble();
		
		System.out.print("Please Enter the Third = ");
		double num3 = sc.nextDouble();
		
		double average = calAvg(num1, num2, num3);
		System.out.println("\nThe Average = " + average);
	}
	
	public static double calAvg(double a, double b, double c) {
		return (a + b + c)/3;
	}
}
Please Enter the first = 99
Please Enter the Second = 124.7
Please Enter the Third = 33.89

The Average = 85.86333333333333