飞道的博客

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

438人阅读  评论(0)
  •         Java经典实战开发第四章的练习题答案

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

  • 
        
    1. public class ZuoYe01 {
    2. public static void main(String[] args) {
    3. //题目一:编写程序求1!+2!+3!+...+30!的和并显示,要求使用方法完成
    4. long sum=leiJia( 30);
    5. System. out.println(sum);
    6. }
    7. public static long leiJia(long a) {
    8. if(a== 1) {
    9. return jieCheng( 1);
    10. }
    11. return leiJia(a -1)+jieCheng(a);
    12. }
    13. public static long jieCheng(long a) { //阶乘的递归
    14. if(a== 1) {
    15. return 1;
    16. }
    17. return jieCheng(a -1)*a;
    18. }
    19. }

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

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

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

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

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

    
        
    1. public class ZuoYe04 {
    2. public static void main(String[] args) {
    3. // 题目四:定义一个正整数的数组,判断最大值和最小值,求出总和
    4. //简易版
    5. int[] array = { 10, 20, 50, 40, 80, 80, 40 };
    6. int max = 0;
    7. int min = 0;
    8. int count = 0;
    9. int zhongfu = 0;
    10. min = max = array[ 0];
    11. for ( int i = 0; i < array.length; i++) {
    12. if ( array[i] > max) {
    13. max = array[i];
    14. }
    15. if ( array[i] < min) {
    16. min = array[i];
    17. }
    18. count += array[i];
    19. }
    20. System.out.print( "数组最大值:" + max + "\n数组最小值:" + min + "\n总和:" + count);
    21. }
    22. }

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


  
  1. import java.util.Scanner;
  2. public class ZuoYe05 {
  3. public static void main(String[] args) {
  4. // 题目五:给出10个整数,然后任意查询一个数字是否在该10个数字里
  5. System. out.println( "请输入一个数字");
  6. Scanner scanner = new Scanner(System. in);
  7. int number=scanner.nextInt(); //用户输入的数字
  8. int count= 0; //次数
  9. int[] array= { 10, 20, 30, 40, 50, 100, 60, 80, 90, 70};
  10. for( int i= 0;i<array.length;i++) {
  11. if(number==array[i]) {
  12. count++;
  13. }
  14. }
  15. if(count> 0) {
  16. System. out.println( "该数字在数组里面");
  17. } else {
  18. System. out.println( "该数字不在数组里面");
  19. }
  20. }
  21. }

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


  
  1. public class ZuoYe06 {
  2. public static void main(String[] args) {
  3. // 题目六:定义一个包含10个元素的数组,对其进行赋值,使每个元素的值等于其下标,然后输出,最后将这个数组(首尾交换)输出
  4. int a[] = new int[ 10]; // 定义一个10个元素的数组
  5. int i; // 数组下标
  6. for (i = 0; i < a.length; i++) {
  7. a[i] = i; // 数组赋值
  8. }
  9. for ( int n : a) { // 打印数组
  10. System. out.print(n + " ");
  11. }
  12. int temp;
  13. for (i = 0; i < (a.length) / 2; i++) {
  14. temp = a[i];
  15. a[i] = a[a.length - 1 - i];
  16. a[a.length - 1 - i] = temp;
  17. }
  18. System. out.println( "\n数组倒置:");
  19. for ( int n : a) {
  20. System. out.print(n + " ");
  21. }
  22. }
  23. }

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


  
  1. public class ZuoYe08 {
  2. // 题目七跟题目四类似,跳过
  3. public static void main(String[] args) {
  4. // 题目8:统计30个0-9之间的数字分别出现多少次
  5. for ( int i = 0; i <= 9; i++) {
  6. chuXian(i);
  7. }
  8. }
  9. public static void chuXian(int x) { // 传递参数判断多少次
  10. 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 };
  11. int count = 0; // 次数
  12. for ( int i = 0; i < array.length; i++) {
  13. if ( array[i] == x) {
  14. count++;
  15. }
  16. }
  17. System.out.println(x + "出现" + count + "次");
  18. }
  19. }

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


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

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


  
  1. public class ZuoYe10 {
  2. public static void main(String[] args) {
  3. // 题目十:在排序好的数组里面添加一个数字,并将其插入到合适的位置
  4. int[] a= { 2, 4, 5, 5, 45, 85, 45, 12};
  5. int[] b=contact(a);
  6. java.util.Arrays.sort(b);
  7. print(b);
  8. }
  9. public static int[] contact(int[]A) {
  10. int x= 35;
  11. int[]B= new int[A.length+ 1];
  12. System.arraycopy(A, 0, B, 0, A.length);
  13. B[A.length]=x;
  14. return B;
  15. }
  16. public static void print(int[] X) {
  17. for( int i= 0;i<X.length;i++) {
  18. System. out.print(X[i]+ " ");
  19. }
  20. }
  21. }

 


转载:https://blog.csdn.net/qq_40286394/article/details/104971882
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场