Java 联系作业1

阶段一模块一作业

1. 提示用户输入年月日信息,判断这一天是这一年中的第几天并打印。

/*
提示用户输入年月日信息,判断这一天是这一年中的第几天并打印。
作者:Xoey
*/
import java.util.Scanner; 
import java.time.LocalDate;
//import java.util.LocalDate; //这个包×
//LocalDate(Java8)对象只包含没有任何时间信息的日期
//Date对象表示特定的日期和时间
public class TheDayInYear {
    
    
	
	public static void main(String[] args) {
    
    
		//1. 提示用户输入年月日信息
		System.out.println("请输入你要判断的日期(xxxx年xx月xx日):");
		Scanner sc = new Scanner(System.in);
		String dateIn = sc.nextLine();
		
		//2. 从输入中提取 年 月 日 的数字
		int count = 0;
		if (dateIn.length() == 11) {
    
    
			int year = Integer.parseInt(dateIn.substring(0, 4));
			int month = Integer.parseInt(dateIn.substring(5, 7));
			int day = Integer.parseInt(dateIn.substring(8, 10));
			LocalDate date = LocalDate.of(year, month, day);
			System.out.println("转换结果:" + year + "-" + month + "-" + day);
		// 3. 判断是否是闰年,计算总天数 
			if (month == 1) {
    
    
				count = day;
			} else if (count == 2) {
    
    
				count = 31 + day;
			} else {
    
    
				if (date.isLeapYear()) {
    
    
					switch (month) {
    
    
					case 3:
						count = 31 + 29 + day;
						break;
					case 4:
						count = 31 + 29 + 31 + day;
						break;
					case 5:
						count = 31 + 29 + 31 + 30 + day;
						break;
					case 6:
						count = 31 + 29 + 31 + 30 + 31 + day;
						break;
					case 7:
						count = 31 + 29 + 31 + 30 + 31 + 30 + day;
						break;
					case 8:
						count = 31 + 29 + 31 + 30 + 31 + 30 + 31 + day;
						break;
					case 9:
						count = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + day;
						break;
					case 10:
						count = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + day;
						break;
					case 11:
						count = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + day;
						break;
					case 12:
						count = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + day;
						break;
					}
				} else {
    
    
					switch (month) {
    
    
					case 3:
						count = 31 + 28 + day;
						break;
					case 4:
						count = 31 + 28 + 31 + day;
						break;
					case 5:
						count = 31 + 28 + 31 + 30 + day;
						break;
					case 6:
						count = 31 + 28 + 31 + 30 + 31 + day;
						break;
					case 7:
						count = 31 + 28 + 31 + 30 + 31 + 30 + day;
						break;
					case 8:
						count = 31 + 28 + 31 + 30 + 31 + 30 + 31 + day;
						break;
					case 9:
						count = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + day;
						break;
					case 10:
						count = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + day;
						break;
					case 11:
						count = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + day;
						break;
					case 12:
						count = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + day;
						break;
					}
				}
			}
		} else {
    
    
			System.out.println("输入的日期格式与要求格式不符!!");
		}
		System.out.println(count);
	}
}
 

import java.util.Scanner;
public class Test01{
    public static void main(String[] args){
        //1.初始化信息
        int[] monthArr = {31,28,31,30,31,30,31,31,30,31,30,31};
        //2.接收用户输入信息
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入年:");
        int year = sc.nextInt();
        if(year%4==0 &&year%100 !=0){//判断是否为闰年
            monthArr[1] = 29;
        }
        System.out.println("请输入月:");
        int month = sc.nextInt();
        if(month<1||month>12){
            System.out.println("月份输入有误");
            return;
        }
        System.out.println("请输入日:");
        int day = sc.nextInt();
        if(day>monthArr[month-1]){
            System.out.println("天数输入有误");return;
        }
        //3.计算是一年中第几天
        int days = 0;
        for(int i=0;i<month-1;i++){
            days+= monthArr[i];
        }
        days += day;
        //4.打印结果
        System.out.println(year+"年"+month+"月"+day+"日是一年中的第"+days+"天");
    }
}

2. 完数

编程找出 1000 以内的所有完数并打印出来。

所谓完数就是一个数恰好等于它的因子之和, 如: 6=1+2+3

/*
编程找出100以内的所有完数并打印出来
*/

public class PerfectNumber{

public static void main(String[] args){
	System.out.println("100以内的所有完数为:");
	for(int i = 1;i<1000;i++){
		int[] arr = new int[i];
		int sum = 0;
		for(int j = 1; j < i; j++){
			if (0==i % j) {
				arr[j] = j;
				sum +=j;				
			}
		}
		if (sum == i){
			System.out.println(i);
		}
			
		}
}

}

3. 双色球

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

/*
实现双色球抽奖游戏中奖号码的生成,中奖号码由 6 个红球号码和 1 个蓝球号码组成。
其中红球号码要求随机生成 6 个 1~33 之间不重复的随机号码。
其中蓝球号码要求随机生成 1 个 1~16 之间的随机号码。
时间:2021/7/9
*/
import java.util.Random;
import java.util.Arrays;
public class ShuangSeqiu {
    
    
	public static void main(String[] args){
    
    
		// 1. 声明和初始化一个数组存放号码数
		int[] arr = new int[7];
		// 定义一个flag用以判断是否重复
		Boolean flag = true;
		//2. 红球号码:随机生成1~33之间不重复的随机号码 
		//. 随机产生一个数,判断该数与之前的数是否重复,如果重复,则重新生成随机数
		for(int i = 0;i<6;i++){
    
    
			do{
    
    
				flag = true;
				Random ra = new Random();
				int temp = ra.nextInt(33)+1; // rand.nextInt(23);中的23是随机数的上限,产生的随机数为0-23的整数,不包括23
				arr[i] = temp;
				//System.out.println(arr[i]);
				for(int j =0;j<i;j++){
    
    
				if (temp==arr[j]){
    
    
					flag = false;
					break; // 如果存在重复,就跳出
				}
			}
			}while(flag==false);
		}
		//3. 蓝球号码要求随机生成 1 个 1~16 之间的随机号码
		Random rb = new Random();
		//int temp1 = ra.nextInt(16)+1;
		arr[6] = rb.nextInt(16)+1;
		
		//4. 打印双色球号码
		System.out.println("双色球号码为:"+Arrays.toString(arr));		
		
	}
}

/**
 * 实现双色球抽奖游戏中奖号码的生成,中奖号码由 6 个红球号码和 1 个蓝球号码组成。
 * 其中红球号码要求随机生成 6 个 1~33 之间不重复的随机号码。
 * 其中蓝球号码要求随机生成 1 个 1~16 之间的随机号码。
 */
public class Test1 {
    
    
    public static void main(String[] args) {
    
    
        System.out.print("红球:");
        //定义一个存放红球的数组
        int [] arr =new int[6];
        //循环6次,用来生成红球的中奖号码
        for (int i = 0; i < 6; i++){
    
    
            //每循环一次就在红球数组 存放一个 1 ~ 33 的中奖号码
            arr[i] = (int) (Math.random()*33+1);
            for (int j = 0; j < i; j++){
    
    
                //判断在红球数组里是否有相同的中奖号码
                if(arr[i] == arr[j]){
    
    
                    //如果有,重新对i赋值,所以i--
                    i--;
                }
            }
        }
        //打印出红球的中奖号码
        for (int i = 0; i < 6; i++){
    
    
            System.out.print(arr[i]+"\t");
        }
        System.out.println();
        //打印出蓝球的中奖号码
        System.out.println("蓝球:"+ (int)(Math.random()*16+1));
    }
}

4. 扩容

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

/*
自定义数组扩容规则,当已存储元素数量达到总容量的 80%时,扩容 1.5 倍。
作者:Xoey
时间:2021/7/10
*/
import java.util.Scanner;
public class ExtendArray{
    
    
	public static void main(String[] args) {
    
    
		// 1.定义一个整形变量用来规定数组长度
		int num = 10;
		// 2.定义一个整形数组
		int[] arr = new int[num];
		// 3.for循环往数组内赋值
		
		System.out.println("请逐个输入数:");
		Scanner sc = new Scanner(System.in);
		// 定义一个flag 判断用户是否继续输入
		boolean flag = false;
		for(int i = 0; i < num; i++) {
    
    
			// 4.判断是否达到储存的80% 如果达到将 arr数组的容量扩大到原先的1.5倍
			String msg = null;
			if(i >= (int)(num * 0.8)){
    
    
				// 5.提示用户是否还要输入数据 如果继续输入则继续扩容 如果不输入则退出
				System.out.println("是否还要继续入,如果继续输入则继续扩容 如果不输入则退出 y/n");
				msg = sc.next();
				if("y".equals(msg)){
    
    
					flag = true;
				}
				if(flag){
    
    
					// 6.建立一个新的数组 相比arr扩大1.5倍
					num = (int) (num * 1.5);
					int[] arrs = new int[num];
					// 7.将arr的元素复制到 Arrs
					System.arraycopy(arr, 0, arrs, 0, arr.length - 1);
					// 8.将arr回收
					arr = arrs;
				}
			}
			// 9.退出扩容
			if("n".equals(msg)){
    
    
				break;
			}
			// 10.存入用户输入的数据
			arr[i] = sc.nextInt();
		}
		// 11.打印数组
		System.out.print("用户输入的数据是");
		for(int i = 0; i < num; i++) {
    
    
			if(arr[i] != 0){
    
    
				System.out.print(" " + arr[i]);
			}
		}
	}
}

public class Test2 {
    
    
    /***
     * 建立一个新数组,长度是原数组长度的1.5倍,并把原来数组的值赋值进去
     * @param arr 原来的数组
     * @return  返回新数组
     */
    public static int[] copyOf(int[] arr){
    
    
        int[] newArr=new int[(int)(arr.length*1.5)];
        //遍历,将原数组储存的数据赋值给新数组
        for(int i=0;i<arr.length;i++){
    
    
            newArr[i]=arr[i];
        }
        return newArr;
    }

    public static void main(String[] args) {
    
    
        Scanner scanner =new Scanner(System.in);
        //提示用户输入初始化数组的大小的整数
        System.out.print("请输入初始化数组的大小:");
        int[] arr =new int[scanner.nextInt()];
        int i = 0;
        while (true){
    
    
            //提示用户给数组赋值
            System.out.print("请给第"+(i+1)+"个元素赋值:");
            arr[i++] =scanner.nextInt();
            //判断已存储元素数量是否达到总容量的80%时
            if(i/(arr.length*1.0) >=0.8){
    
    
                //如达到总容量的80%时,则调用copyOf()方法,并打印出当前数组的值
                arr = copyOf(arr);
                System.out.println(Arrays.toString(arr));
            }
        }

    }
}

5. 棋盘

使用双重循环实现五子棋游戏棋盘的绘制, 棋盘界面的具体效果如下:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-e5z0pw7J-1625991608224)(https://s0.lgstatic.com/i/image/M00/7E/8F/Ciqc1F_POZKAFXPEAAE80Qr3KdQ067.png)]

/*使用双重循环实现五子棋游戏棋盘的绘制
*/

public class DoubleFor{
    
    
	public static void main(String[] args){
    
    
		//1、看到数字和字母的一瞬间,第一反应是用asc码打印,ASCII 48~57,97~102,43,0
		//2、两个for循环,第一个for循环中:第0个打印0,然后循环10次48~57,再循环10次97~120
		//创建数组用以遍历
		int[] arr = {
    
    0,48,49,50,51,52,53,54,55,56,57,97,98,99,100,101,102};
		for(int i=0;i<arr.length;i++){
    
    
			for(int j=0;j<arr.length;j++){
    
    
				//第一行打印数组顺序内容
				if(0==i){
    
    
					char num = (char)arr[j];
					System.out.print(num+" ");
				//第一列打印数组顺序内容
				}else if(0==j){
    
    
					char num = (char)arr[i];
					System.out.print(num+" ");
				}else{
    
    
					System.out.print("+" +" ");
				}
			}
			System.out.println();
			
			
		}
	}
}
public class Test5_1 {
    
    
    public static void main(String[] args) {
    
    
        for (int i = 0; i <= 16; i++) {
    
    
            for (int j = 0; j <= 16; j++) {
    
    
                if`){
    
    
                    System.out.print("   ");
                }else if(0 == i && j <= 10) {
    
    
                    System.out.print((j-1) + "  ");
                }else if(0 == i && j <= 16) {
    
    
                    System.out.print((char)(j-11+'a') + "  ");
                }else if(0 == j && i <= 10) {
    
    
                    System.out.print((i-1) + "  ");
                }else if(0 == j && i <= 16) {
    
    
                    System.out.print((char)(i-11+'a') + "  ");
                }else {
    
    
                    System.out.print("+  ");
                }
            }
            System.out.println();
        }
    }
}
           System.out.print((char)(j-11+'a') + "  ");
            }else if(0 == j && i <= 10) {
                System.out.print((i-1) + "  ");
            }else if(0 == j && i <= 16) {
                System.out.print((char)(i-11+'a') + "  ");
            }else {
                System.out.print("+  ");
            }
        }
        System.out.println();
    }
}

}


猜你喜欢

转载自blog.csdn.net/weixin_39107270/article/details/118655789
今日推荐