Java经典实战开发第四章的练习题答案

  •         Java经典实战开发第四章的练习题答案

题目一:编写程序求1!+2!+3!+...+30!的和并显示,要求使用方法完成

  • public class ZuoYe01 {
    
    	public static void main(String[] args) {
    	//题目一:编写程序求1!+2!+3!+...+30!的和并显示,要求使用方法完成
    		long sum=leiJia(30);
    		System.out.println(sum);
    	}
    	public static long leiJia(long a) {
    		if(a==1) {
    			return jieCheng(1);
    		}
    		return leiJia(a-1)+jieCheng(a);
    	}
    	
    	public static long  jieCheng(long a) {//阶乘的递归
    		if(a==1) {
    			return 1;
    		}
    		return jieCheng(a-1)*a;
    	}
    }

    题目二 :给一个整数的数组,求出其中的奇数和偶数的个数

    public class ZuoYe02 {
    
    	public static void main(String[] args) {
    		// 题目二:给一个整数的数组,求出其中的奇数和偶数的个数
    		int[] array = { 10, 20, 55, 45, 20 };
    		int jishu = 0;
    		int oushu = 0;
    		for (int i = 0; i < array.length; i++) {
    			if (array[i] % 2 == 0) {
    				oushu++;
    
    			} else {
    				jishu++;
    			}
    
    		}
    		System.out.println("偶数的个数是" + oushu + "\t" + "奇数的个数是" + jishu);
    	}
    
    }
    

     题目三: int[] olderArray={0,2,13,1,21,20,1,0,0,4,1};除去了为0的项,将不为0再弄成一个数组

    public class ZuoYe03 {
    
    	public static void main(String[] args) {
    		// 题目三: int[] olderArray={0,2,13,1,21,20,1,0,0,4,1};除去了为0的项,将不为0再弄成一个数组
    		 int[] oldArray={0,2,13,1,21,20,1,0,0,4,1};
    		 for (int i = 0;i<oldArray.length;i++) {
    			 if(oldArray[i]!=0) {
    				 int[] newArray =  {oldArray[i]};
    				 for (int newarray : newArray) {
    					 System.out.print(newarray+"\t");
    			 }
    			
    				
    			}
    		}
    	}
    
    }
    

    题目四:定义一个正整数的数组,判断最大值和最小值,求出总和

    public class ZuoYe04 {
    
    	public static void main(String[] args) {
    		// 题目四:定义一个正整数的数组,判断最大值和最小值,求出总和
    		//简易版
    		int[] array = { 10, 20, 50, 40, 80, 80, 40 };
    		int max = 0;
    		int min = 0;
    		int count = 0;
    		int zhongfu = 0;
    		min = max = array[0];
    		for (int i = 0; i < array.length; i++) {
    
    			if (array[i] > max) {
    				max = array[i];
    			}
    			if (array[i] < min) {
    				min = array[i];
    			}
    			count += array[i];
    		}
    		System.out.print("数组最大值:" + max + "\n数组最小值:" + min + "\n总和:" + count);
    	}
    	
    }
    

    题目五:给出10个整数,然后任意查询一个数字是否在该10个数字里

import java.util.Scanner;

public class ZuoYe05 {

	public static void main(String[] args) {
		// 题目五:给出10个整数,然后任意查询一个数字是否在该10个数字里
		System.out.println("请输入一个数字");
		Scanner scanner = new Scanner(System.in);
		int number=scanner.nextInt();//用户输入的数字
		int count=0;//次数
		int[] array= {10,20,30,40,50,100,60,80,90,70};
		for(int i=0;i<array.length;i++) {
			if(number==array[i]) {
				count++;
			}
		}
		if(count>0) {
			System.out.println("该数字在数组里面");
		}else {
			System.out.println("该数字不在数组里面");
		}
	}

}

题目六 :题目六:定义一个包含10个元素的数组,对其进行赋值,使每个元素的值等于其下标,然后输出,最后将这个数组(首尾交换)输出

public class ZuoYe06 {

	public static void main(String[] args) {
		// 题目六:定义一个包含10个元素的数组,对其进行赋值,使每个元素的值等于其下标,然后输出,最后将这个数组(首尾交换)输出
		int a[] = new int[10];// 定义一个10个元素的数组
		int i;// 数组下标
		for (i = 0; i < a.length; i++) {
			a[i] = i;// 数组赋值
		}
		for (int n : a) {// 打印数组
			System.out.print(n + " ");
		}
		int temp;
		for (i = 0; i < (a.length) / 2; i++) {
			temp = a[i];
			a[i] = a[a.length - 1 - i];
			a[a.length - 1 - i] = temp;
		}
		System.out.println("\n数组倒置:");
		for (int n : a) {
			System.out.print(n + " ");
		}
	}
}

题目八:统计30个0-9之间的数字分别出现多少次

public class ZuoYe08 {
	// 题目七跟题目四类似,跳过
	public static void main(String[] args) {
		// 题目8:统计30个0-9之间的数字分别出现多少次
		for (int i = 0; i <= 9; i++) {
			chuXian(i);
		}
	}

	public static void chuXian(int x) {// 传递参数判断多少次
		int[] array = { 0, 0, 0, 0, 0, 1, 2, 5, 6, 4, 4, 4, 5, 4, 4, 6, 4, 6, 4, 8, 7, 9, 4, 1, 9, 2, 6, 4, 5, 4 };
		int count = 0;// 次数
		for (int i = 0; i < array.length; i++) {
			if (array[i] == x) {
				count++;
			}
		}

		System.out.println(x + "出现" + count + "次");
	}
}

题目九:定义一个正整数数组,保存10个元素,利用程序将最大值保存在第一个元素的操作

public class ZuoYe09 {

	public static void main(String[] args) {
		// 题目九:定义一个正整数数组,保存10个元素,利用程序将最大值保存在第一个元素的操作
		// 可以这样理解为:利用冒泡排序将数组由大到小排列
		int[] array = { 10, 20, 45, 585, 541, 6541, 64, 4654, 645, 5646, 4564 };
		int temp;// 交换数
		for (int i = 0; i < array.length - 1; i++) {// 比较轮数
			for (int j = 0; j < array.length - 1 - i; j++) {// 比较数组里面元素的比较次数
				if (array[j + 1] > array[j]) {
					temp = array[j];
					array[j] = array[j + 1];
					array[j + 1] = temp;
				}
			}
		}
		for (int i : array) {
			System.out.print(i + "\t");
		}
	}

}

 题目十:在排序好的数组里面添加一个数字,并将其插入到合适的位置

public class ZuoYe10 {

	public static void main(String[] args) {
		// 题目十:在排序好的数组里面添加一个数字,并将其插入到合适的位置
		int[] a= {2,4,5,5,45,85,45,12};
		int[] b=contact(a);
		java.util.Arrays.sort(b);
		print(b);

	}
	public static int[] contact(int[]A) {
		int x= 35;
		int[]B=new int[A.length+1];
		System.arraycopy(A, 0, B, 0, A.length);
		B[A.length]=x;
		return B;
	}
	public static void print(int[] X) {
		for(int i=0;i<X.length;i++) {
			System.out.print(X[i]+" ");
		}
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值