前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >那些年,我们一起做过的 Java 课后练习题(31 - 35)

那些年,我们一起做过的 Java 课后练习题(31 - 35)

作者头像
村雨遥
发布2022-06-15 11:10:22
2660
发布2022-06-15 11:10:22
举报
文章被收录于专栏:JavaPark

实例 31

题目

将一个数组逆序输出。

分析

输入多个元素存入数组,然后逆序遍历数组输出即可;

实现

代码语言:javascript
复制
import java.util.Arrays;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 *
 * @author : cunyu
 * @version : 1.0
 * @email : 747731461@qq.com
 * @website : https://cunyu1943.github.io
 * @date : 2021/6/4 13:51
 * @project : Java 编程实例
 * @package : PACKAGE_NAME
 * @className : Example31
 * @description :
 */

public class Example31 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int[] arr = new int[10];
        for (int i = 0; i < 10; i++) {
            System.out.println("输入第 " + (i + 1) + " 个元素!");
            arr[i] = scanner.nextInt();
        }

        System.out.println("输入的数组为:" + Arrays.toString(arr));
        System.out.println("逆序输出的数组");
        for (int i = arr.length - 1; i >= 0; i--) {
            System.out.print(arr[i] + "\t");
        }
    }
}

结果

实例 32

题目

取一个整数 a 从右端开始的 4 ~ 7 位。

分析

输入一个整数,然后将其转换为字符串,然后遍历输出从右端开始的 4 ~ 7 位即可!

实现

代码语言:javascript
复制
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 *
 * @author : cunyu
 * @version : 1.0
 * @email : 747731461@qq.com
 * @website : https://cunyu1943.github.io
 * @date : 2021/6/4 13:56
 * @project : Java 编程实例
 * @package : PACKAGE_NAME
 * @className : Example32
 * @description :
 */

public class Example32 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入一个整数");
        int num = scanner.nextInt();
        String str = Integer.toString(num);
        System.out.println("整数从右端开始的 4 ~ 7 位:");
        for (int i = str.length() - 4; i >= str.length() - 7; i--) {
            System.out.print(str.charAt(i));
        }
    }
}

结果

实例 33

题目

打印杨辉三角。

分析

仔细观察杨慧三角可以看到:

第 0 列和对角线上的数据全部为 1,其余位置上的数据为上一行正对数据与上一行正对前一个数据之和。

比如:a[4][2] = a[3][2] + a[3][1]

实现

代码语言:javascript
复制
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 *
 * @author : cunyu
 * @version : 1.0
 * @email : 747731461@qq.com
 * @website : https://cunyu1943.github.io
 * @date : 2021/6/4 14:01
 * @project : Java 编程实例
 * @package : PACKAGE_NAME
 * @className : Example33
 * @description :
 */

public class Example33 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入杨辉三角的行数");
        int row = scanner.nextInt();
        int[][] triangle = new int[row][];
        System.out.println("杨辉三角为:");
        for (int i = 0; i < triangle.length; i++) {
            triangle[i] = new int[i + 1];

            for (int j = 0; j < triangle[i].length; j++) {
                if (i == 0 || j == 0 || i == j) {
                    triangle[i][j] = 1;
                } else {
                    triangle[i][j] = triangle[i - 1][j] + triangle[i - 1][j - 1];
                }
                System.out.print(triangle[i][j] + " ");
            }
            System.out.println();
        }
    }
}

结果

实例 34

题目

输入 3 个数 a、b、c,按大小顺序输出。

分析

输入 3 个数存入数组中,然后利用 Arrays.sort() 进行排序,最后按照从小到到输出即可。

实现

代码语言:javascript
复制
import java.util.Arrays;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 *
 * @author : cunyu
 * @version : 1.0
 * @email : 747731461@qq.com
 * @website : https://cunyu1943.github.io
 * @date : 2021/6/4 14:06
 * @project : Java 编程实例
 * @package : PACKAGE_NAME
 * @className : Example34
 * @description :
 */

public class Example34 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入三个整数");
        int[] arr = new int[3];
        for (int i = 0; i < 3; i++) {
            System.out.println("输入第 " + (i + 1) + " 个整数");
            arr[i] = scanner.nextInt();
        }
        Arrays.sort(arr);
        System.out.println("从小到大的输出顺序:");
        for (int i = 0; i < arr.length; i++) {
            if (i != arr.length - 1) {
                System.out.print(arr[i] + " < ");
            } else {
                System.out.print(arr[i]);
            }
        }
    }
}

结果

实例 35

题目

输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。

分析

将数组复制到新的数组中,然后找到最大的元素和最小元素的索引,然后再到原数组中交换最大元素和第一个元素,最小元素和最后一个元素。

实现

代码语言:javascript
复制
import java.util.Arrays;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 *
 * @author : cunyu
 * @version : 1.0
 * @email : 747731461@qq.com
 * @website : https://cunyu1943.github.io
 * @date : 2021/6/4 14:15
 * @project : Java 编程实例
 * @package : PACKAGE_NAME
 * @className : Example35
 * @description :
 */

public class Example35 {
    public static int SIZE = 10;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int[] arr = new int[SIZE];
        for (int i = 0; i < SIZE; i++) {
            System.out.println("输入第 " + (i + 1) + " 个元素");
            arr[i] = scanner.nextInt();
        }
        int[] newArr = Arrays.copyOf(arr, arr.length);
        Arrays.sort(newArr);

        System.out.println("输入的数组:" + Arrays.toString(arr));

//        找到最大元素和最小元素的索引位置
        int minIndex = 0;
        int maxIndex = 0;
        for (int i = 0; i < SIZE; i++) {
            if (arr[i] == newArr[0]) {
                minIndex = i;
            }
            if (arr[i] == newArr[SIZE - 1]) {
                maxIndex = i;
            }
        }

//        交换最大元素和第一个元素
        int tmp1 = arr[0];
        arr[0] = arr[maxIndex];
        arr[maxIndex] = tmp1;

//        交换最小元素和最后一个元素
        int tmp2 = arr[arr.length - 1];
        arr[arr.length - 1] = arr[minIndex];
        arr[minIndex] = tmp2;

        System.out.println("交换后的数组:" + Arrays.toString(arr));
    }
}

结果

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-06-09,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 实例 31
    • 题目
      • 分析
        • 实现
          • 结果
          • 实例 32
            • 题目
              • 分析
                • 实现
                  • 结果
                  • 实例 33
                    • 题目
                      • 分析
                        • 实现
                          • 结果
                          • 实例 34
                            • 题目
                              • 分析
                                • 实现
                                  • 结果
                                  • 实例 35
                                    • 题目
                                      • 分析
                                        • 实现
                                          • 结果
                                          领券
                                          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档