java基础编程题

博客围绕Java基础编程题展开,包含计算一组数据的平均值、中位数、方差,利用重载求2、3个数中的较大值,以及求n个数的和、最大值、最小值等内容,并给出了相应计算结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

java基础编程题

1. 计算一组数据的平均值,中位数,以及方差

public class Emoployee {
	
	//定义一个整型变量名为pay的数组,用来存放数据,初始值为null;
	
	private int[] pay = null;
	//无参构造方法
	public Emoployee() {
		
	}
	
	//带一个参数的构造方法
	public Emoployee(int i) {
		pay = new int[i];	
	}
	//给数组赋值的方法
	public void setEmoployee(int i,int pay) {
		this.pay[i] = pay;
	}
	
	//求平均值的方法
	public double getAvg() {
		double sum = 0;
		for (int i = 0; i < pay.length; i++) {
			sum = sum + pay[i];
		}
		return (double)(sum/pay.length);
		//整型数据和整型数据做算术运算时,得到的结果还是整型,
		//我们做除法运算时,难免会有小数的存在,
		//而整型数据小数点后都为0,所以我们要加double进行强制转换,才可以保留小数位
	}
	
	//遍历数组输出方法
	public void select() {
		
		for (int i = 0; i < pay.length; i++) {
			System.out.print(pay[i]+"\t");
		}
		System.out.println();
	}
	
	//排序,冒泡方法
	public  void maopao(){
		for (int i = 0; i < pay.length-1; i++) {
			for (int j = 0; j < pay.length-i-1; j++) {
				if (pay[j]>pay[j+1]) {
					int temp;
					temp = pay[j];
					pay[j] = pay[j+1];
					pay[j+1] = temp;
				}
			}
		}
	}

	
	//求中位数的方法,此方法要考虑数组长度为奇数还是偶数,奇数就取中间值,偶数就取中间两个数的平均值
	public  double beginend(){
		int i = pay.length/2;
		if (pay.length % 2 == 0) {		
			return (double)(pay[i-1]+pay[i])/2;//为什么是i-1和i,因为数组索引从0 开始啊
		}else {				
			return pay[i];
		}
	}
	
	//计算标准差,样本标准差=方差的算术平方根=s=sqrt(((x1-x)^2 +(x2-x)^2 +......(xn-x)^2)/(n-1))
	
	public  double numcount(double avg){

		double sum = 0;
		for (int i = 0; i < pay.length; i++) {
			 sum = sum + Math.pow((pay[i]-avg),2);//Math.pow(a,b)求a的b次幂	
		}
		
		double resulet = sum/pay.length;
		return Math.sqrt(resulet);//Math.sqrt(a),求a的算术平方根
	}

	public static void main(String[] args) {
		
		//new一个长度为5的数组,此处new对象的时候调用的带参数的构造方法,是为了准确的让数组长度可以确定,更加灵活。
		Emoployee emoployee = new Emoployee(5);
		//这里输入的数据为1,2,3,4,5(方便计算),如果你上面new的数组长度为n,你下面就要添加多少条数据。
		emoployee.setEmoployee(0, 1);
		emoployee.setEmoployee(1, 3);
		emoployee.setEmoployee(2, 2);
		emoployee.setEmoployee(3, 5);
		emoployee.setEmoployee(4, 4);
		//调用getAvg()方法并将返回值赋给avg
		System.out.println("初始数组为:");
		//调用遍历输出数组方法
		emoployee.select();
		
		System.out.println("排序后的数组为:");
		//冒泡排序数组
		emoployee.maopao();
		//调用遍历输出数组方法
		emoployee.select();
		
		double avg = emoployee.getAvg();
		System.out.println("平均值为:"+avg);//平均值为:3      

		//调用求中位数方法
		double zws = emoployee.beginend();
		System.out.println("中位数为:"+zws);
		
		//调用求标准差方法
		double bzc = emoployee.numcount(avg);
		System.out.println("标准差为:"+bzc);
	}

}

结果:

image-20201117191540808

方法2. 计算一组数据的平均值,中位数,以及方差

/**  
 * @ClassName: Wre
 * @Description: 求平均值、中位数、标准差
 * @author Tao
 * @date 2020年11月15日 
*/

public class Wre {
		public static void main(String[] args) {					
//			int[] arr = new int[]{6,5,4,3,2,1,7,8,9,10,11,12,13,14,15,19,17,18,74,20,80,22,29,24};
//			int[] arr = new int[]{6,5,2,4,7};
//			int[] arr = new int[]{6,5,2,4,7,8};
			int[] arr = new int[]{6,5,4,3,2,1};
//          int[] arr = new int[]{5,4,3,2,1};
			//输出初始数组
			System.out.print("最初的数组为:");
			for (int i = 0; i < arr.length; i++) {
				System.out.print(arr[i]+" ");
			}
			System.out.println();//用于换行	
			
			//求中位数要对数组先进行排序,调用排序方法
			maopao(arr);
			//输出排序后的数组
			System.out.print("排序后的数组为:");
			for (int i = 0; i < arr.length; i++) {
				System.out.print(arr[i]+" ");
			}
			System.out.println();//用于换行
			
			//调用求和方法
			int sum = sum(arr);
			//调用求平均值方法
			double avg = (double)sum/arr.length;
			//整型数据和整型数据做算术运算时,得到的结果还是整型,
			//我们做除法运算时,难免会有小数的存在,
			//而整型数据小数点后都为0,所以我们要加double进行强制转换,才可以保留小数位
			System.out.println("平均值为"+avg);
			
			//调用求中位数方法
			double zws = beginend(arr);
			System.out.println("中位数为:"+zws);
			
			//调用求标准差方法
			double bzc = numcount(arr, avg);
			System.out.println("标准差为"+bzc);
			
		}
		
		//一下都是要用到的方法,因为都是工具类,用static修饰比较方便,需要使用的时候可以直接调用,不用通过对象调用。
		

		//排序,冒泡方法
		public static void maopao(int[] arr){
			for (int i = 0; i < arr.length-1; i++) {
				for (int j = 0; j < arr.length-i-1; j++) {
					if (arr[j]>arr[j+1]) {
						int temp;
						temp = arr[j];
						arr[j] = arr[j+1];
						arr[j+1] = temp;
					}
				}
			}
		}
		
		//求和方法
		public static int sum(int[] arr){
			int sum = 0;
			for (int i = 0; i < arr.length; i++) {
				sum = sum + arr[i];
			}
			return sum;
		}
		
		//求中位数的方法,此方法要考虑数组长度为奇数还是偶数,奇数就取中间值,偶数就取中间两个数的平均值
		public static double beginend(int[] arr){
			int i = arr.length/2;
			if (arr.length % 2 == 0) {		
				return (double)(arr[i-1]+arr[i])/2;//为什么是i-1和i,因为数组索引从0 开始啊
			}else {				
				return arr[i];
			}
		}
		
		//计算标准差,样本标准差=方差的算术平方根=s=sqrt(((x1-x)^2 +(x2-x)^2 +......(xn-x)^2)/(n-1))
		
		public static double numcount(int[] arr,double avg){

			double sum = 0;
			for (int i = 0; i < arr.length; i++) {
				 sum = sum + Math.pow((arr[i]-avg),2);//Math.pow(a,b)求a的b次幂	
			}
			
			double resulet = sum/arr.length;
			return Math.sqrt(resulet);//Math.sqrt(a),求a的算术平方根
		}
}

结果:

当数组长度为偶数时:

image-20201117165022050

当数组长度为奇数时:

image-20201117165107271

2. 利用重载求2、3个数中的较大值

//求2个数、3个数中最大的数
//重载的实现:两同一不同
//两同: a.在同一个类下 b.方法名相同
//一不同:参数列表不同--a:参数个数不同,b:参数类型不同
public class TestNum {


	public static void main(String[] args) {
		
		int a = test(2,3);
		int b = test(2,8,4);
		System.out.println("2,3中较大值为:"+a);
		System.out.println("2,8,4中较大值为:"+b);

	}
	
	public static int test(int a,int b){
		int max = a;
		if(a>b){
			max = a;
		}else{
			max =b;
		}
		return max;
	}
	
    //参数列表不同--参数个数不同,先用a和b作比较,取出较大者再来和c比较,取得最大值
	public static int test(int a,int b,int c){
		int max = a;
		if(a>b){
			max = a;
			if(max>c){
				max =a;
			}else{
				max = c;
			}
		}else{
			max =b;
			if (max>c) {
				max = b;
			}else {
				max = c;
			}
		}
		return max;
	}

}

结果:

image-20201117170513431

3. 求n个数的和,以及其中最大值、最小值。

import java.util.Scanner;

/**  
 * @ClassName: DecimalManager
 * @Description:求n个数的和,以及其中最大值、最小值。
 * @author Tao
 * @date 2020年11月15日 
*/

public class DecimalManager {

	public static void main(String[] args) {
		
		System.out.println("请确定你要输入几个数:");
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		int[] arr = new int[n];
		System.out.println("请输入你要输入的"+n+"个数:");
		for (int i = 0; i < arr.length; i++) {
			arr[i] = scanner.nextInt();
		}
		
		int sum = sum(arr);
		int max = max(arr);
		int min = min(arr);
		
		System.out.println("和为:"+sum+"\n最大值为:"+max+"\n最小值为:"+min);
		

	}

	//求和
	public static int sum(int[] arr){
		int sum = 0;
		for (int i = 0; i < arr.length; i++) {
			sum = sum + arr[i];
		}
		return sum;
	}
	
	//求最大值
	public static int max(int[] arr){
		int max = arr[0];
		for (int i = 0; i < arr.length; i++) {
			if (arr[i]>max) {
				max=arr[i];
			}
		}
		return max;
	}
	
	//求最小值
	public static int min(int[] arr) {
		int min =arr[0];
		for (int i = 0; i < arr.length; i++) {
			if (arr[i]<min) {
				min=arr[i];
			}
		}
		return min;
	}
}

结果:
image-20201117170307724

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值