Java从入门到精通章节练习题——第五章

Exercise 1 数独

package chapter5;

/**
 * 数独
 */
public class Exercise1 {
    public static void main(String[] args) {
        int[][] arr = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                System.out.print(arr[i][j]);
            }
            System.out.println();
        }
        System.out.println("对角线之和= " + (arr[0][0] + arr[1][1] + arr[2][2]));
        System.out.println("对角线之和= " + (arr[0][2] + arr[1][1] + arr[2][0]));
        System.out.println("列之和= " + (arr[0][0] + arr[1][0] + arr[2][0]));
        System.out.println("列之和= " + (arr[0][1] + arr[1][1] + arr[2][1]));
        System.out.println("列之和= " + (arr[0][2] + arr[1][2] + arr[2][2]));
        System.out.println("行之和= " + (arr[0][0] + arr[0][1] + arr[0][2]));
        System.out.println("行之和= " + (arr[1][0] + arr[1][1] + arr[1][2]));
        System.out.println("行之和= " + (arr[2][0] + arr[2][1] + arr[2][2]));
    }
}

Exercise 2 矩阵转置

package chapter5;

/**
 * 矩阵转置
 */
public class Exercise2 {
    public static void main(String[] args) {
        int[][] array = {{91, 25, 8},
                        {56, 14, 2},
                        {47, 3, 67}};
        int[][] newArray = new int[3][3];
        System.out.println("原数组");
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array.length; j++) {
                System.out.print(array[i][j] + " ");
            }
            System.out.println();
        }
        System.out.println("转置后数组");
        //循环的将旧数组按列的顺序取出,赋给新数组,则完成行、列转置
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array.length; j++) {
                newArray[i][j] = array[j][i];//行与列交换就是i与j的交换
                System.out.print(newArray[i][j] + " ");
            }
            System.out.println();
        }
    }
}

Exercise 3 杨辉三角

package chapter5;

/**
 * 杨辉三角
 */
public class Exercise3 {
    public static void main(String[] args) {
        //思路:1.每行的第一个和最后一个数都为1;
        //     2.其他数为上一行相同位置的数与它前一个数的和
        int arr[][] = new int[10][10];//创建一个10*10的二维数组
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j <= i; j++) {//每行数字的个数与行数相等
                if (j == 0 || j == i) {//条件判断,第一个和最后一个数为1
                    arr[i][j] = 1;
                } else {
                    //其他数为上一行相同位置的数与它前一个数的和
                    arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j];
                }
                System.out.print(arr[i][j] + " ");//输出
            }
            System.out.println();
        }
    }
}

Exercise 4 推箱子游戏

1.该代码未设置箱子推到终点时验证,码友们可自行优化;
2.为方便起见,将原题字母改为整数
3.每移动一次重新刷新输出一次地图

package chapter5;

import java.util.Scanner;

/**
 * 推箱子游戏
 */
@SuppressWarnings({"all"})
public class Exercise4 {
    public static void main(String[] args) {
        //规定: 1 为墙壁, 2 为自己, 3 为箱子, 5为终点
        //设置图形
        int[][] map = new int[8][10];

        //设置自己初始位置
        int x = 1;
        int y = 1;
        map[x][y] = 2;

        //设置箱子初始位置
        int m = 2;
        int n = 2;
        map[m][n] = 3;

        //设置终点位置
        int c = 6;
        int v = 5;
        map[c][v] = 5;

        //设置图形循环显示条件
        boolean loop = true;

        //设置上下两边
        for (int i = 0; i < 10; i++) {
            map[0][i] = 1;
            map[7][i] = 1;
        }

        //设置左右两边
        for (int i = 0; i < 8; i++) {
            map[i][0] = 1;
            map[i][9] = 1;
        }

        //设置原图形障碍墙壁
        for (int i = 0; i < 4; i++) {
            map[i][3] = 1;
        }
        map[2][5] = 1;
        map[3][5] = 1;
        map[3][6] = 1;
        map[3][8] = 1;
        map[4][8] = 1;
        map[5][4] = 1;
        map[5][5] = 1;
        map[5][6] = 1;
        map[6][4] = 1;

        //输出当前图形
        while (loop) {
            for (int i = 0; i < 8; i++) {
                for (int j = 0; j < 10; j++) {
                    System.out.print(map[i][j] + " ");
                }
                System.out.println();
            }
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入WASD移动");
            char key = scanner.next().charAt(0);
            //自己移动
            switch (key) {
                case 'w':
                    //如果下一步碰到墙壁则提示,并继续循环输出地图重新移动
                    if (map[x - 1][y] == 1) {
                        System.out.println("无路可走");
                        //return;
                    } else if (map[x - 1][y] == 3 && map[m - 1][n] == 0) {//如果下一步碰到箱子 3,再判断箱子的下一步是否可走
                        //碰到箱子后,则将原来箱子位置置为 2,箱子的下一步置为 3
                        map[x][y] = 0;
                        x -= 1;
                        map[x][y] = 2;
                        m -= 1;
                        map[m][n] = 3;
                    } else {
                        map[x][y] = 0;
                        x -= 1;
                        map[x][y] = 2;
                    }
                    break;
                case 'a':
                    //如果下一步碰到墙壁则提示,并继续循环输出地图重新移动
                    if (map[x][y - 1] == 1) {
                        System.out.println("无路可走");
                        //return;
                    } else if (map[x][y - 1] == 3 && map[m][n - 1] == 0) {//如果下一步碰到箱子 3,再判断箱子的下一步是否可走
                        //碰到箱子后,则将原来箱子位置置为 2,箱子的下一步置为 3
                        map[x][y] = 0;
                        y -= 1;
                        map[x][y] = 2;
                        n -= 1;
                        map[m][n] = 3;
                    } else {
                        map[x][y] = 0;
                        y -= 1;
                        map[x][y] = 2;
                    }
                    break;
                case 's':
                    //如果下一步碰到墙壁则提示,并继续循环输出地图重新移动
                    if (map[x + 1][y] == 1) {
                        System.out.println("无路可走");
                        //return;
                    } else if (map[x + 1][y] == 3 && map[m + 1][n] == 0) {//如果下一步碰到箱子 3,再判断箱子的下一步是否可走
                        //碰到箱子后,则将原来箱子位置置为 2,箱子的下一步置为 3
                        map[x][y] = 0;
                        x += 1;
                        map[x][y] = 2;
                        m += 1;
                        map[m][n] = 3;
                    } else {
                        map[x][y] = 0;
                        x += 1;
                        map[x][y] = 2;
                    }
                    break;
                case 'd':
                    //如果下一步碰到墙壁则提示,并继续循环输出地图重新移动
                    if (map[x][y + 1] == 1) {
                        System.out.println("无路可走");
                        //return;
                    } else if (map[x][y + 1] == 3 && map[m][n + 1] == 0) {//如果下一步碰到箱子 3,再判断箱子的下一步是否可走
                        //碰到箱子后,则将原来箱子位置置为 2,箱子的下一步置为 3
                        map[x][y] = 0;
                        y += 1;
                        map[x][y] = 2;
                        n += 1;
                        map[m][n] = 3;
                    } else {
                        map[x][y] = 0;
                        y += 1;
                        map[x][y] = 2;
                    }
                    break;
            }
        }
    }
}

Exercise 5 五子棋游戏

未设置游戏结束功能,只有简单的输入实现。

package chapter5;

import java.util.Scanner;

/**
 * 五子棋游戏
 */
public class Exercise5 {

    public static void main(String[] args) {
        //设定 1 为自己,2 为对手棋子
        int[][] map = new int[11][11];

        boolean loop = true;

        for (int i = 0; i < map.length; i++) {
            map[0][i] = i - 1;
        }

        for (int i = 0; i < map.length; i++) {
            map[i][0] = i - 1;
        }

        map[0][0] = 0;

        //显示菜单
        do {
            System.out.println("--------------------");
            for (int i = 0; i < map.length; i++) {
                for (int j = 0; j < map.length; j++) {
                    System.out.print(map[i][j] + "\t");
                }
                System.out.println();
            }
            System.out.println("--------------------");
            //输入坐标己方开始下棋
            Scanner scannerX1 = new Scanner(System.in);
            System.out.println("请输入横坐标");
            int x1 = scannerX1.nextInt();
            Scanner scannerY1 = new Scanner(System.in);
            System.out.println("请输入纵坐标");
            int y1 = scannerY1.nextInt();

            //输入后设置输入位置数字为 1
            map[x1 + 1][y1 + 1] = 1;

            //自己输入完成后显示当前地图
            System.out.println("--------------------");
            for (int i = 0; i < map.length; i++) {
                for (int j = 0; j < map.length; j++) {
                    System.out.print(map[i][j] + "\t");
                }
                System.out.println();
            }
            System.out.println("--------------------");

            //对手输入
            Scanner scannerX2 = new Scanner(System.in);
            System.out.println("请输入横坐标");
            int x2 = scannerX2.nextInt();
            Scanner scannerY2 = new Scanner(System.in);
            System.out.println("请输入纵坐标");
            int y2 = scannerY2.nextInt();

            //输入后设置输入位置数字为 2
            map[x2 + 1][y2 + 1] = 2;

        } while (loop);

        //对手下棋

    }


}

Exercise 6 统计学生成绩

package chapter5;

import java.util.Scanner;

/**
 * 统计学生成绩
 */
public class Exercise6 {
    public static void main(String[] args) {
        int num = 1;//学生编号

        double avg1 = 0;//平均成绩

        double sum = 0;//总成绩

        double chGra = 0;//语文成绩

        double maGra = 0;//数学成绩

        double enGra = 0;//英语成绩

        double[][] doubles = new double[3][6];
        for (num = 1; num <= 3; num++) {
            //接收输入信息
            Scanner scanner1 = new Scanner(System.in);
            System.out.println("请输入第 " + num + " 个学生的编号");
            num = scanner1.nextInt();
            Scanner scanner2 = new Scanner(System.in);
            System.out.println("请输入语文成绩");
            chGra = scanner2.nextDouble();
            Scanner scanner3 = new Scanner(System.in);
            System.out.println("请输入数学成绩");
            maGra = scanner3.nextDouble();
            Scanner scanner4 = new Scanner(System.in);
            System.out.println("请输入英语成绩");
            enGra = scanner4.nextDouble();

            sum = chGra + maGra + enGra;//总分

            double avg = (chGra + maGra + enGra) / 3;//平均分

            //将平均数保留 2 小数
            String str = String.format("%.2f", avg);
            avg1 = Double.parseDouble(str);

            //赋值
            doubles[num - 1][0] = num;
            doubles[num - 1][1] = chGra;
            doubles[num - 1][2] = maGra;
            doubles[num - 1][3] = enGra;
            doubles[num - 1][4] = avg1;
            doubles[num - 1][5] = sum;


        }
        System.out.println("学生成绩结果如下");
        System.out.println("----------------------------------");
        //表头信息
        System.out.println("学生编号\t\t\t" + "语文成绩\t\t\t" + "数学成绩\t\t\t" +
                "英语成绩\t\t\t" + "平均成绩\t\t\t" + "总成绩\t\t\t");

        //循环输出信息
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 6; j++) {
                System.out.print(doubles[i][j] + "\t\t\t\t");
            }
            System.out.println();
        }
    }
}

Exercise 7 模拟客车售票

package chapter5;

import java.util.Scanner;

/**
 * 模拟客车售票
 */
public class Exercise7 {
    public static void main(String[] args) {
        // 0 表示该座位未占用,1 表示该座位已占用
        //座位用 4 * 9 的二维数组表示
        //创建座位
        int seat[][] = new int[9][4];
        //设置售完票条件变量
        boolean loop = true;

        //循环展示当前座位情况
        do {

            System.out.println("\t客车售票系统");
            System.out.println("9排4列的大巴车开始售票");

            //显示当前座位情况
            for (int i = 0; i < seat.length; i++) {
                for (int j = 0; j < seat[i].length; j++) {
                    System.out.print(seat[i][j] + "\t");
                }
                System.out.println();
            }

            //开始订座
            //当用户输入座位坐标时将该位置数字设为 1
            //1.接受用户输入
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入座位行数");
            int row = scanner.nextInt();
            System.out.println("请输入座位列数");
            int col = scanner.nextInt();

            //2.选好后改变座位显示
            seat[row][col] = 1;
            System.out.println("定座成功");
        } while (loop);
    }
}

Exercise 8 自动批卷程序

package chapter5;

import java.util.Arrays;
import java.util.Scanner;

/**
 * 自动批卷程序
 */
public class Exercise8 {
    public static void main(String[] args) {
        //学生成绩
        String studentScore[][] = {{"A", "B", "C", "D", "E", "F", "G", "A", "A", "A"},
                                    {"A", "B", "C", "D", "E", "F", "G", "A", "A", "A"},
                                    {"A", "B", "C", "D", "E", "F", "G", "A", "A", "A"},
                                    {"A", "B", "C", "D", "E", "F", "G", "A", "A", "A"},
                                    {"A", "B", "C", "D", "E", "F", "G", "A", "A", "A"},
                                    {"A", "B", "C", "D", "E", "F", "G", "A", "A", "A"},
                                    {"A", "B", "C", "D", "E", "F", "G", "A", "A", "A"},
                                    {"A", "B", "C", "D", "E", "F", "G", "A", "A", "A"},};

        //正确成绩
        String correctScore[] = {"B", "A", "D", "C", "C", "B", "C", "A", "D", "B",};

        System.out.println("请输入想查询成绩的学生编号:");
        Scanner scanner = new Scanner(System.in);
        int num = scanner.nextInt();

        String oneStudentScore[] = studentScore[num - 1];//将取出同学的答案设成一维数组

        System.out.println("---------正确答案为:" + Arrays.toString(correctScore));
        System.out.println("第 " + num + " 个同学的答案为: " + Arrays.toString(oneStudentScore));

        int correctNum = 0;//正确个数
        //判断正确个数,遍历答案数组,有相同计数器加 1
        for (int i = 0; i < oneStudentScore.length; i++) {
            if (oneStudentScore[i].equals(correctScore[i]))
                correctNum++;
        }

        System.out.println("正确的个数为:" + correctNum);

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

earlytrain9653

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值