java数组类练习题 及答案 (内含杨辉三角的打印)

package com.swpu.homework;

/**
 * 给定一个数组arr1 = {1,6,3,9,10,45,100},要求利用程序实现数组的倒序,并且将数组输出出来。 arr1倒序数组为arr2 =
 * {100,45,10,9,3,6,1}
 * 
 * 分析 arry1[0]=arry2[6] arry1[1]=arry2[5] 即是 arry1[i]=arry2[6-i]
 * 
 */
public class Hwork1 {
    public static void main(String[] args) {
        int[] arry1 = new int[] { 1, 6, 3, 9, 10, 45, 100 };
        int[] arry2 = new int[arry1.length];
        for (int i = 0; i < arry1.length; i++) {
            arry2[6 - i] = arry1[i];
        }
        for (int i = 0; i < arry1.length; i++) {
            System.out.println(arry2[i]);
        }
    }
}
 

 

 

package com.swpu.homework;

import java.util.Scanner;

/**
 * 循环录入一个有10个学生的班级成绩,求出班级里面的平均分、最高分、最低分。以及求出班级中成绩高于80分的人数并输出。
 * 
 * 
 */
public class Hwork2 {

    public static void main(String[] args) {
        int grade;// 学生成绩
        int[] arry = new int[10];
        Scanner scanner = new Scanner(System.in);
        for (int i = 0; i < 10; i++) {
            System.out.println("请输入第" + (i + 1) + "个学生的成绩:");
            grade = scanner.nextInt();
            arry[i] = grade;

        }
        way1(arry);
    }

    private static void way1(int[] arry) {// 先使用冒泡排序法排序 后进行运算
        int sum = 0;
        int count = 0;
        double avg;
        for (int i = 0; i < arry.length - 1; i++) {// 冒泡排序
            for (int j = 0; j < arry.length - i - 1; j++) {
                int tmp;
                if (arry[j] > arry[j + 1]) {
                    tmp = arry[j];
                    arry[j] = arry[j + 1];
                    arry[j + 1] = tmp;
                }
            }

        }
        for (int i = 0; i < arry.length; i++) {// 循环打印输入数组
            System.out.println(arry[i]);
            sum += arry[i];// 求总和
            if (arry[i] > 80) {// 求总分大于80分的人数
                count++;
            }
        }
        System.out.println("最高分为:" + arry[arry.length - 1]);
        System.out.println("最低分为:" + arry[0]);
        System.out.println("分数大于80的人有:" + count);
        System.out.println("平均分为:" + (sum / 10));

    }

    public static void way(int[] arry) {// 普通的方法
        int max = arry[0];// 定义最高分
        int min = arry[0];// 定义最低分
        int sum = 0;
        double avg;// 定义平均分
        int count = 0;// 定义大于80分的人数
        for (int i = 0; i < arry.length; i++) {
            sum += arry[i];// 累加求的总成绩
            if (arry[i] < min) {
                min = arry[i];
            }
            if (arry[i] > max) {
                max = arry[i];
            }
            if (arry[i] > 80) {
                count++;
            }
        }
        System.out.println("最高分为:" + max);
        System.out.println("最低分为:" + min);
        System.out.println("分数大于80的人有:" + count);
        System.out.println("平均分为:" + (sum / 10));
    }
}
 

 

package com.swpu.homework;

import java.util.Arrays;

//* 题目:打印出杨辉三角形(要求打印出10行如下图) 程序分析:
//*1                                                                1个数字
//*1    1                                                            2*
//*1    2    1                                                        3*
//*1      3    3    1                                                    4*
//*1    4    6    4    1                                                5*
//*1    5    10    10    5    1                                            6*
//*/
public class Hwork3 {
    public static void main(String[] args) {
        int[][] arry = new int[10][10];// 定义十行十列的数组
        for (int i = 0; i < arry.length; i++) {
            for (int j = 0; j < arry[i].length; j++) {
                arry[i][0] = arry[i][i] = 1;// 每行第一个和最后一个数 值为一
                if (i > 1 && j > 0 && j < i) {
                    arry[i][j] = arry[i - 1][j] + arry[i - 1][j - 1];// 上一行 相同列 加上 上一行 上一列
                }
            }
        }
        for (int i = 0; i < arry.length; i++) {// 遍历数组进行输出
            for (int j = 0; j < arry[i].length; j++) {
                if (arry[i][j] != 0)
                    System.out.print(arry[i][j] + " ");
            }
            System.out.println();
        }
    }
}
 

 

package com.swpu.homework;

import java.util.Scanner;

/**
 * 
 * @author Swpu 使用二维数组实现一个有10个班级,每个班级最多30人的学生JAVA成绩循环录入以及求出每个班级的平均分
 *         以及整个学校的最高分以及最低分。并统计整个学校中高于80分的人数总和并输出相关信息。
 * 
 */
public class Hwork6 {
    public static void main(String[] args) {
        int[][] ary = new int[10][30];
        inputScore(ary);// 输入每个人的分数,保存到数组
        printAvg(ary);// 打印平均分
    }

    private static void printAvg(int[][] ary) {
        int index = 0;// 计算分数大于80的人数
        int max = ary[0][0];// 最高分
        int min = ary[0][0];// 最低分
        for (int i = 0; i < ary.length; i++) {
            int sum = 0;// 班级总分
            for (int j = 0; j < ary[i].length; j++) {
                if (ary[i][j] > 80) {// 计算大于80分的人数
                    index++;
                }
                if (max < ary[i][j]) {// 计算最大值
                    max = ary[i][j];
                }
                if (min > ary[i][j]) {// 计算最小值
                    min = ary[i][j];
                }
                int score = ary[i][j];
                sum += score;
            }
            System.out.println((i + 1) + "班的平均分为:" + sum * 1.0 / ary[i].length);
        }
        System.out.println("学校中最高分为:" + max);
        System.out.println("学校中最低分为:" + min);
        System.out.println("学校中分数大于80的人有:" + index);
    }

    private static void inputScore(int[][] ary) {
        Scanner input = new Scanner(System.in);//
        for (int i = 0; i < ary.length; i++) {
            System.out.println("\t\t" + (i + 1) + "班成绩情况");
            for (int j = 0; j < ary[i].length; j++) {
                System.out.print("第" + (j + 1) + "号的成绩:");
                int score = input.nextInt();// 输入成绩 并且把他存储到数组里边
                ary[i][j] = score;
            }
        }
    }
}
 

 

package com.swpu.homework;

import java.util.Arrays;

/**
 * 
 * @author Swpu 实现3人斗地主的发牌程序,发牌后输出甲乙丙三方的扑克牌,假如谁的扑克牌有炸弹,必须输出有几幅炸弹
 *         要求:(输出的扑克牌不需要花色,分别为:$大王 ¥小王 2 3 4 5 6 7 8 9 10 J Q K A)。
 * 
 * 
 */
public class Hwork7 {
    public static void main(String[] args) {
        // 三人就座
        Object[] people1 = new Object[17];
        Object[] people2 = new Object[20];
        Object[] people3 = new Object[17];
        // 一副新牌
        Object[] array = new Object[] { 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8,
                8, 9, 9, 9, 9, 10, 10, 10, 10, 'J', 'J', 'J', 'J', 'Q', 'Q', 'Q', 'Q', 'K', 'K', 'K', 'K', 'A', 'A',
                'A', 'A', "大王", "小王" };
        array = shuffle(array, 10000);// 洗牌
        // 发牌
        int p1 = 0, p2 = 0, p3 = 0;
        for (int i = 0; i < 51; i = i + 3) {
            people1[p1] = array[i];
            p1++;
            people2[p2] = array[i + 1];
            p2++;
            people3[p3] = array[i + 2];
            p3++;
        }
        for (int i = 17; i < 20; i++) {
            people2[i] = array[i + 34];
        }
        // 整理牌
        System.out.println(Arrays.toString(people1));
        System.out.println(Arrays.toString(people2));
        System.out.println(Arrays.toString(people3));
        System.out.println(zha(people1) + zha(people2) + zha(people3));
    }

    public static Object[] shuffle(Object[] array, int shuffleNum) {// 洗牌
        int a, b;
        for (int i = 0; i < shuffleNum; i++) {
            a = (int) (Math.random() * 54 - 1);
            b = (int) (Math.random() * 54 - 1);
            Object temp = array[a];
            array[a] = array[b];
            array[b] = temp;
        }
        return array;
    }

    public static int zha(Object[] people) {
        int za = 0, z = 0;
        for (int i = 0; i < people.length; i++) {
            if (people[i].equals("大王")) {
                z++;
            }
            if (people[i].equals("小王")) {
                z++;
            }
        }
        if (z == 2) {
            za++;
        }
        for (int i = 0; i < people.length; i++) {
            for (int j = 0; j < people.length; j++) {
                int num = 0;
                if (people[i].equals(people[j])) {
                    num++;
                }
                if (num == 4) {
                    za++;
                }
            }
        }
        return za;
    }

}
 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值