6_实例

实例一

/*
 * 提示用户输入年月日信息,判断这一天是这一年中的第几天并打印。
 * */
package cn.itcast.day01.homework;

import java.util.Scanner;

public class Answer1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("输入年份:");
        int year = sc.nextInt();
        System.out.print("输入月份:");
        int month = sc.nextInt();
        System.out.print("输入日期:");
        int day = sc.nextInt();

        int[] monthDaysArr = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        if (0 == year % 4) { //闰年
            monthDaysArr[1] = 29;
        }
        if (day > monthDaysArr[month - 1] || month > 12) {
            System.out.println("一年最多只有12个月,且不能超过本月最大天数!");
        } else {
            int sumDays = 0;
            for (int i = 0; i < month - 1; i++) {
                sumDays += monthDaysArr[i + 1];
            }
            sumDays += day;
            System.out.println("这是" + year + "年的第 " + sumDays + " 天");
        }
    }
}

//执行结果:
/*
输入年份:2020
输入月份:9
输入日期:19
这是2020年的第 262 天
*/

实例二

/*
 * 编程找出 1000 以内的所有完数并打印出来。
 * 所谓完数就是一个数恰好等于它的因子之和,如:6=1+2+3
 * */

package cn.itcast.day01.homework;

public class Answer2 {
    public static void main(String[] args) {
        System.out.print("1000以内的完数有:");
        for (int i = 1; i < 1000 + 1; i++) {
            int sum_yushu = 0;
            for (int j = 1; j < i; j++) {
                if (0 == i % j) {
                    sum_yushu += j;
                }
            }
            if (i == sum_yushu) {
                System.out.print(i);
                System.out.print(' ');
            }
        }
    }
}
//执行结果:
/*
1000以内的完数有:6 28 496 
*/

实例三

/*
 * 实现双色球抽奖游戏中奖号码的生成,中奖号码由 6 个红球号码和 1 个蓝球号码组成。
 * 其中红球号码要求随机生成 6 个 1~33 之间不重复的随机号码。
 * 其中蓝球号码要求随机生成 1 个 1~16 之间的随机号码。
 * */
package cn.itcast.day01.homework;

import java.util.Arrays;
import java.util.Random;

public class Answer3 {
    public static void main(String[] args) {
        // --------------------------- 红球 ---------------------------
        Random ran = new Random();
        int[] redBalls = new int[6];
        for (int i = 0; i < 6; i++) {
            while (0 == redBalls[i]) { //把0全部换掉
                redBalls[i] = ran.nextInt(33);
            }
            if (0 == i) {
                continue;
            }
            int j = 0;
            while (j < i) { //去重
                if (0 == redBalls[i] || redBalls[i] == redBalls[j]) { //当前数字为0 或 前面有重复数字,则更换当前数字
                    redBalls[i] = ran.nextInt(33);
                } else { //没有重复数据,指针j前移1位
                    j++;
                }
            }
        }
        // --------------------------- 蓝球 ---------------------------
        int blueBall = 0;
        while (0 == blueBall) {
            blueBall = ran.nextInt(16);
        }

        int[] doubleCollerBall = new int[7];
        System.arraycopy(redBalls, 0, doubleCollerBall, 0, redBalls.length);
        doubleCollerBall[6] = blueBall;

        System.out.print("双色球的中奖号码是:");
        System.out.println(Arrays.toString(doubleCollerBall));
    }
}

//执行结果:
/*
双色球的中奖号码是:[25, 12, 26, 30, 8, 14, 12]
*/

实例四

/*
 * 自定义数组扩容规则,当已存储元素数量达到总容量的 80%时,扩容 1.5 倍。
 * 例如,总容量是 10,当输入第 8 个元素时,数组进行扩容,容量从 10 变 15。
 * */
package cn.itcast.day01.homework;

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

public class Answer4 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        boolean flag = true;
        int[] arr = new int[1];
        int count = 0;
        while (flag) {
            System.out.print("请输入数字:");
            int input = sc.nextInt();
            arr[count] = input;

            if (count >= (arr.length - 1) * 0.8) {
                int[] brr = new int[(int) ((arr.length * 1.5 > arr.length * 15 / 10) ? (arr.length * 15 / 10 + 1) : (arr.length * 15 / 10))];
                System.arraycopy(arr, 0, brr, 0, arr.length);
                arr = brr;
            }
            count ++;
            System.out.println("数组长度:" + arr.length);
            System.out.println("元素数量:" + count);
            System.out.println("数组内容:" + Arrays.toString(arr));
        }
    }
}

//执行结果:
/*
**************请输入数字:1
数组长度:2
元素数量:1
数组内容:[1, 0]
**************请输入数字:2
数组长度:3
元素数量:2
数组内容:[1, 2, 0]
**************请输入数字:3
数组长度:5
元素数量:3
数组内容:[1, 2, 3, 0, 0]
**************请输入数字:4
数组长度:5
元素数量:4
数组内容:[1, 2, 3, 4, 0]
**************请输入数字:5
数组长度:8
元素数量:5
数组内容:[1, 2, 3, 4, 5, 0, 0, 0]
**************请输入数字:6
数组长度:8
元素数量:6
数组内容:[1, 2, 3, 4, 5, 6, 0, 0]
**************请输入数字:7
数组长度:12
元素数量:7
数组内容:[1, 2, 3, 4, 5, 6, 7, 0, 0, 0, 0, 0]
**************请输入数字:8
数组长度:12
元素数量:8
数组内容:[1, 2, 3, 4, 5, 6, 7, 8, 0, 0, 0, 0]
**************请输入数字:9
数组长度:12
元素数量:9
数组内容:[1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0]
**************请输入数字:10
数组长度:18
元素数量:10
数组内容:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 0, 0, 0, 0, 0, 0, 0]
**************请输入数字:
	......
*/

实例五

/*
 * 使用双重循环实现五子棋游戏棋盘的绘制。
 * */
package cn.itcast.day01.homework;

public class Answer5 {
    public static void main(String[] args) {
        int[] arr = {48, 49, 50,51,52,53,54,55,56,57,97,98,99,100,101,102};
        for (int i = 0; i < arr.length; i++) {
            if (0 == i) {
                System.out.print(' ');
                System.out.print(' ');
                for (int k = 0; k < arr.length; k++) {
                    System.out.print(' ');
                    System.out.print((char)arr[k]);
                    System.out.print(' ');
                }
                System.out.println();
            }
            System.out.print((char) arr[i]);
            System.out.print(' ');
            for (int j = 0; j < arr.length; j++) {

                System.out.print(" + ");
            }
            System.out.println();
        }
    }
}
//执行结果:
/*
   0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f 
0  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
1  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
2  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
3  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
4  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
5  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
6  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
7  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
8  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
9  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
a  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
b  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
c  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
d  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
e  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
f  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  + 
*/
posted @ 2020-09-21 22:00  咕噜噜~  阅读(220)  评论(0编辑  收藏  举报